當前位置: 首頁>>代碼示例>>Java>>正文


Java Oauth2Scopes類代碼示例

本文整理匯總了Java中com.google.api.services.oauth2.Oauth2Scopes的典型用法代碼示例。如果您正苦於以下問題:Java Oauth2Scopes類的具體用法?Java Oauth2Scopes怎麽用?Java Oauth2Scopes使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Oauth2Scopes類屬於com.google.api.services.oauth2包,在下文中一共展示了Oauth2Scopes類的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: TestApp

import com.google.api.services.oauth2.Oauth2Scopes; //導入依賴的package包/類
public TestApp() {
	try
	{
		HttpTransport httpTransport = new NetHttpTransport();             
		JacksonFactory jsonFactory = new JacksonFactory();
		//ComputeCredential credential = new ComputeCredential.Builder(httpTransport, jsonFactory).build();	
		GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport,jsonFactory);				            
		if (credential.createScopedRequired())
		    credential = credential.createScoped(Arrays.asList(Oauth2Scopes.USERINFO_EMAIL));           				            
		Oauth2 service = new Oauth2.Builder(httpTransport, jsonFactory, credential)
		            .setApplicationName("oauth client")   
		            .build();				            
		Userinfoplus ui = service.userinfo().get().execute();
		System.out.println(ui.getEmail());



         // Using Google Cloud APIs
	  Storage  storage_service = StorageOptions.defaultInstance().service();
       
         Iterator<Bucket> bucketIterator = storage_service.list().iterateAll();
         while (bucketIterator.hasNext()) {
           System.out.println(bucketIterator.next());
         }	

	} 
	catch (Exception ex) {
		System.out.println("Error:  " + ex);
	}
}
 
開發者ID:salrashid123,項目名稱:gcpsamples,代碼行數:31,代碼來源:TestApp.java

示例2: TestApp

import com.google.api.services.oauth2.Oauth2Scopes; //導入依賴的package包/類
public TestApp() {
	try
	{
		HttpTransport httpTransport = new NetHttpTransport();             
		JacksonFactory jsonFactory = new JacksonFactory();

           // unset GOOGLE_APPLICATION_CREDENTIALS
           //String SERVICE_ACCOUNT_JSON_FILE = "YOUR_SERVICE_ACCOUNT_JSON_FILE.json";
           //FileInputStream inputStream = new FileInputStream(new File(SERVICE_ACCOUNT_JSON_FILE));
           //GoogleCredential credential = GoogleCredential.fromStream(inputStream, httpTransport, jsonFactory);

		// to use application default credentials and a JSON file, set the environment variable first:
           // export GOOGLE_APPLICATION_CREDENTIALS=YOUR_SERVICE_ACCOUNT_JSON_FILE.json        
           GoogleCredential credential = GoogleCredential.getApplicationDefault(httpTransport,jsonFactory);

           if (credential.createScopedRequired())
               credential = credential.createScoped(Arrays.asList(Oauth2Scopes.USERINFO_EMAIL));

		Oauth2 service = new Oauth2.Builder(httpTransport, jsonFactory, credential)
		            .setApplicationName("oauth client")   
		            .build();				            
		Userinfoplus ui = service.userinfo().get().execute();
		System.out.println(ui.getEmail());


         // Using Google Cloud APIs with service account file
	  // You can also just export an export GOOGLE_APPLICATION_CREDENTIALS and use StorageOptions.defaultInstance().service()
	  // see: https://github.com/google/google-auth-library-java#google-auth-library-oauth2-http
	  /*
	  Storage storage_service = StorageOptions.newBuilder()
		.setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream("/path/to/your/certificate.json")))
		.build()
		.getService();			
	  */

         // Using Google Cloud APIs

	  Storage  storage_service = StorageOptions.defaultInstance().service();
       
         Iterator<Bucket> bucketIterator = storage_service.list().iterateAll();
         while (bucketIterator.hasNext()) {
           System.out.println(bucketIterator.next());
         }		
	} 
	catch (Exception ex) {
		System.out.println("Error:  " + ex);
	}
}
 
開發者ID:salrashid123,項目名稱:gcpsamples,代碼行數:49,代碼來源:TestApp.java


注:本文中的com.google.api.services.oauth2.Oauth2Scopes類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。