【问题标题】:How to store credentials for Google Drive SDK locally如何在本地存储 Google Drive SDK 的凭据
【发布时间】:2013-09-19 01:32:23
【问题描述】:

我正在开发一个基于 Java 的桌面应用程序,它需要从用户的 Google Drive 帐户下载一些文件。我研究了the Google Drive SDK documentation,到目前为止我已经想出了以下代码:

public class Main
{
  public static void main(String[] args)
  {
    String clientId = "...";
    String clientSecret = "...";

    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
      httpTransport, 
      jsonFactory,
      clientId,
      clientSecret,
      Arrays.asList(DriveScopes.DRIVE)
    )
      .setAccessType("online")
      .setApprovalPrompt("auto").build();

    String redirectUri = "urn:ietf:wg:oauth:2.0:oob";     
    String url = 
      flow
        .newAuthorizationUrl()
        .setRedirectUri(redirectUri)
        .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 response = 
      flow
        .newTokenRequest(code)
        .setRedirectUri(redirectUri)
        .execute();

    GoogleCredential credential = 
      new GoogleCredential()
        .setFromTokenResponse(response);

    Drive service = 
      new Drive.Builder(httpTransport, jsonFactory, credential)
        .build();

    ...
  }
}

这可行,但它要求用户每次都对应用程序进行授权(即在浏览器中打开给定的 URL 并复制授权令牌)。我需要以一种仅在第一次运行时要求用户授权的方式来实现该应用程序。然后应用程序将在本地存储某种秘密令牌以供下次使用。

我已经彻底研究了文档,但我没有找到任何关于如何实现这一目标的充分解释(尤其是在桌面应用程序中)。

我该怎么做?

【问题讨论】:

    标签: java oauth-2.0 google-drive-api


    【解决方案1】:

    第1步:使用离线访问类型生成URL

    flow = new GoogleAuthorizationCodeFlow.Builder(
    httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
    .setAccessType("offline")
    .setApprovalPrompt("auto").build();
    String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
    

    第 2 步:存储凭证 accessToken 和 refreshToken 。 code = 上述网址的响应代码

    GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
                    .setJsonFactory(jsonFactory)
                    .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
                    .build()
                    .setFromTokenResponse(response);
    String accessToken = credential.getAccessToken();
    String refreshToken = credential.getRefreshToken();
    

    第 3 步:在需要时重复使用令牌

    GoogleCredential credential1 = new GoogleCredential.Builder().setJsonFactory(jsonFactory)
         .setTransport(httpTransport).setClientSecrets(CLIENT_ID, CLIENT_SECRET).build();
    credential1.setAccessToken(accessToken);
    credential1.setRefreshToken(refreshToken);
    Drive service = new Drive.Builder(httpTransport, jsonFactory, credential1).build();
    

    第 4 步:了解 OAuth 以处理错误和刷新令牌

    【讨论】:

      【解决方案2】:

      http://javadoc.google-api-java-client.googlecode.com/hg/1.7.0-beta/jdiff/Google%20API%20Client%20Library%20for%20Java%201.7.0-beta/com/google/api/client/googleapis/auth/oauth2/GoogleAuthorizationCodeFlow.html 阅读有关 accessType = "offline" 的信息。这将返回一个刷新令牌,这是您正在寻找的可存储的“秘密令牌”。保存它,您可以将其转换为 Auth Token 而无需任何用户干预。

      【讨论】:

      • 能否请您更具体地说明如何更新代码?我似乎无法让它工作。提前致谢!
      • 在您的流程中添加 .setAccessType("offline")...构建。这里有一个例子stackoverflow.com/questions/10294124/getting-null-refresh-token。或者,您可以直接调用 URL,如此处所述developers.google.com/accounts/docs/OAuth2InstalledApp
      • 为什么要让用户打开浏览器 url 并复制令牌?为什么不 urlfetch 呢?
      • @ZigMandel 我不明白 urlfetching 令牌是什么意思。你能解释一下吗?
      • 查看关于客户端类型流程的 google oauth 文档。那里有例子。
      猜你喜欢
      • 2014-09-20
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      相关资源
      最近更新 更多