1

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

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 并复制授权令牌)。我需要以一种仅在用户第一次运行时才需要授权的方式来实现该应用程序。然后,应用程序将在本地存储某种秘密令牌以供下次使用。

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

我怎么做?

4

2 回答 2

3

第 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 。代码 = 上述网址的响应代码

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 以处理错误和刷新令牌

于 2014-12-09T09:07:09.803 回答
0

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"。这将返回一个刷新令牌,这是您正在寻找的可存储的“秘密令牌”。保存它,您可以将其转换为身份验证令牌,而无需任何用户干预。

于 2013-09-14T15:11:06.427 回答