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

标签 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 并复制授权 token )。我需要以一种仅在用户第一次运行时才要求用户授权的方式来实现该应用程序。然后,应用程序会在本地存储某种 secret token 以备下次使用。

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

我该怎么做?

最佳答案

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

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 步:在需要时重复使用 token

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

关于java - 如何在本地存储 Google Drive SDK 的凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18801948/

相关文章:

google-apps-script - 使用 Google 应用程序脚本解析 XML 文件(存储在 GoogleDrive 上)

java - Java中每种类型的对象类

Java DNS 缓存查看器

java - 如何在 GridPane 中对齐 ListView 基线

java - 使用 OAuth2.0 或 Azure Active Directory 保护 REST API

salesforce - 为什么 Salesforce OAuth2 将我从一个实例 na3 for ex 重定向到另一个 na9

oauth - OAuth 是否总是假定用户界面?

java - 我应该把提取所有类别并使其可见的代码放在哪里

python - 在 Google Drive SDK 中使用实时更新方法的示例?

google-drive-api - 谷歌驱动器 API : Cannot copy a shared file