【问题标题】:Stored Credential from Google API to be reused using Java来自 Google API 的存储凭证可使用 Java 重用
【发布时间】:2013-11-20 14:06:56
【问题描述】:

我已成功授权我的桌面应用程序。它存储一个名为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();

如何读取存储的凭据并绕过命令行提示符?

我在想这样的事情:

if (stored cred exists) { 
    try  {
        // use 
    } catch  {
        // prompt the user
    }
}

【问题讨论】:

  • 基本上有办法说 GoogleCredential credential = new GoogleCredential.Builder().getThingFromFile
  • 我在这里stackoverflow.com/a/42517728/266531 发布了一个答案,我认为可以更详细地解决您所追求的问题

标签: java google-api google-api-java-client


【解决方案1】:
public Credential buildEmptyCredential() {
    Credential credential = null;
    try {
        credential = new GoogleCredential.Builder()
            .setClientSecrets(Utils.getClientCredential())
            .setTransport(new NetHttpTransport())
            .setJsonFactory(Constant.JSON_FACTORY)
            .build();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return credential;
}

buildEmptyCredential()
    .setRefreshToken(storedCredential.getRefreshToken())
    .setAccessToken(storedCredential.getAccessToken())
    .setExpirationTimeMilliseconds(storedCredential.getExpirationTimeMilliseconds());

【讨论】:

  • 您可以为这个答案添加一些解释
【解决方案2】:

您可以像这样从存储的凭据创建 GoogleCredential 对象:

GoogleCredential credential = new GoogleCredential.Builder()
        .setTransport(new NetHttpTransport())
        .setJsonFactory(new JacksonFactory())
        .setClientSecrets("client_id", "client_secret")
        .build();

credential.setAccessToken("access_token");

【讨论】:

  • 它如何知道从哪里加载刷新令牌或StroredCredential
猜你喜欢
  • 1970-01-01
  • 2021-10-12
  • 1970-01-01
  • 1970-01-01
  • 2016-09-06
  • 2012-07-31
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
相关资源
最近更新 更多