Kil*_*ler 11 java google-api google-api-java-client
我已成功授权我的桌面应用程序.它存储一个名为的文件StoredCredential.为了不必每次运行应用程序时都将复制URL保存到浏览器,接受等步骤,我想使用我已存储的凭据.
我的代码到目前为止:
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport,
JSON_FACTORY,
CLIENT_ID,
CLIENT_SECRET,
SCOPE)
.setDataStoreFactory(dataStoreFactory)
.setApprovalPrompt("auto").setAccessType("offline").build();
//
System.out.println("flow success");
String url = flow
.newAuthorizationUrl()
.setRedirectUri(REDIRECT_URI)
.build();
System.out.println("Please open the following URL in your browser then "
+ "type the authorization code:");
System.out.println(" " + url);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();
GoogleTokenResponse tokenResponse
= flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential
.Builder()
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.addRefreshListener(new CredentialRefreshListener() {
@Override
public void onTokenResponse(
Credential credential,
TokenResponse tokenResponse) throws IOException {
System.out.println("Token Refreshed Succesfully");
}
@Override
public void onTokenErrorResponse(
Credential credential,
TokenErrorResponse tokenErrorResponse) throws IOException {
System.out.println("ERROR WITH TOKEN WTF?");
}})
.build();
Run Code Online (Sandbox Code Playgroud)
如何读取存储的凭据并绕过命令行提示符?
我想的是:
if (stored cred exists) {
try {
// use
} catch {
// prompt the user
}
}
Run Code Online (Sandbox Code Playgroud)
Jha*_*nvi 14
您可以使用存储的凭据创建GoogleCredential对象,如下所示:
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setClientSecrets("client_id", "client_secret").build();
credential.setAccessToken("access_token");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6278 次 |
| 最近记录: |