終於設法得到它的工作 萬一有人面臨着同樣的問題
創建谷歌帳戶,生成JSON文件(對本地應用包括客戶端ID,客戶端密鑰和重定向 將JSON文件到您的應用程序 爲以下
public class AuthorizationHelper {
public static Credential authorize() throws Exception {
java.io.File DATA_STORE_DIR =
new java.io.File("permissionsFile", "PermissionsFolder");
JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
// load client secrets
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(new FileInputStream("client_secrets.json")));
if (clientSecrets.getDetails().getClientId().startsWith("Enter")
|| clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println(
"Enter Client ID and Secret from https://code.google.com/apis/console/?api=plus "
+ "into plus-cmdline-sample/src/main/resources/client_secrets.json");
System.exit(1);
}
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets,
Collections.singleton("https://www.google.com/m8/feeds/contacts/default/full")).setDataStoreFactory(
dataStoreFactory).build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("googleAccount");
}
}
創建類似的方式憑證,其中permissionsFile和permissionsFolder是文件和客戶端ID)文件夾,暫時將存儲你試圖用 Google賬戶登錄的帳戶您的權限 - 您所創建的帳戶(我不使用@ gmail.com)
然後就去做
myService.setOAuth2Credentials(AuthorizationHelper.authorize());
並刪除
contactsService.setUserCredentials(email, password);
因爲你不需要在你的代碼中的登錄名和密碼。
您將被重定向到登錄頁面瀏覽器,並要求輸入用戶名和passord
希望幫助別人
感謝
Ievgenii