这是"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));