如何在本地存储Google Drive SDK的凭据

时间:2013-09-14 12:53:53

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

我正在开发一个基于Java的桌面应用程序,需要从用户的Google云端硬盘帐户下载一些文件。我研究过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并复制授权令牌)。我需要以一种方式实现应用程序,这种方式要求用户在第一次运行时对其进行授权。然后,应用程序将在本地存储某种秘密令牌以供下次使用。

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

我该怎么做?

2 个答案:

答案 0 :(得分: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。 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以处理错误和刷新令牌

答案 1 :(得分: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”的信息。这将返回一个刷新令牌,这是您正在寻找的可存储的“秘密令牌”。保存此项,您可以将其转换为Auth Token而无需任何用户干预。