【发布时间】:2015-06-19 21:11:37
【问题描述】:
这是"Java Quickstart" Gmail API 教程中给出的代码。这是我为应用程序创建凭据需要做的事情:
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, clientSecrets, Arrays.asList(SCOPE))
.setAccessType("online")
.setApprovalPrompt("auto").build();
String url = flow.newAuthorizationUrl().setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI)
.build();
System.out.println("Please open the following URL in your browser then type"
+ " the authorization code:\n" + url);
// Read code entered by user.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = null;
try {
code = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
// Generate Credential using retrieved code.
GoogleTokenResponse response = null;
try {
response = flow.newTokenRequest(code)
.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI).execute();
} catch (IOException e) {
e.printStackTrace();
}
GoogleCredential credential = new GoogleCredential()
.setFromTokenResponse(response);
我可以做些什么来自动化上述过程,就像在这里完成的那样,以获得进一步使用的凭证?
以下示例适用于 Google 任务。
GoogleAccountCredential credential =
GoogleAccountCredential.usingOAuth2(this, Collections.singleton(TasksScopes.TASKS));
【问题讨论】: