【问题标题】:How to authenticate Google Drive without requiring the user to copy/paste auth code?如何在不要求用户复制/粘贴验证码的情况下验证 Google Drive?
【发布时间】:2012-09-13 08:04:31
【问题描述】:

我正在使用 DriveCommandLine 应用程序来学习 Drive API。我只是想知道是否可以使用 Google Drive 对我的桌面应用程序进行身份验证,而无需用户从浏览器复制/粘贴身份验证代码?而是只是将令牌从浏览器传回应用程序?我可以使用 Dropbox API 和 Google Documents List API 做到这一点,但无法弄清楚如何使用 Google Drive API 来实现这一点。

谢谢。

Google Drive API - DriveCommandLine 示例应用(稍作修改):

public class DriveCommandLine {

  private static String CLIENT_ID = APPCONSTANTS.Google.CONSUMER_KEY;
  private static String CLIENT_SECRET = APPCONSTANTS.Google.CONSUMER_SECRET;

  private static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

  public static void main(String[] args) throws IOException, URISyntaxException {
    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();

    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
        httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
        .setAccessType("offline")
        .setApprovalPrompt("force").build();

    String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
    System.out.println("Enter authorization code:");
    Desktop.getDesktop().browse(new URI(url));
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String code = br.readLine();

    GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
    GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);

    //Create a new authorized API client
    Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();
}

Google 文档列表 API:

    public void authenticate(){
            GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
            oauthParameters.setOAuthConsumerKey(APPCONSTANTS.Google.CONSUMER_KEY);

            OAuthSigner signer;
            if (APPCONSTANTS.Google.USE_RSA_SIGNING) {
                    signer = new OAuthRsaSha1Signer(APPCONSTANTS.Google.CONSUMER_SECRET);
            } else {
                oauthParameters.setOAuthConsumerSecret(APPCONSTANTS.Google.CONSUMER_SECRET);
                signer = new OAuthHmacSha1Signer();
            }

            GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(signer);

            oauthParameters.setScope(APPCONSTANTS.Google.SCOPES);

            oauthHelper.getUnauthorizedRequestToken(oauthParameters);

            String requestUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters);

            Desktop desktop = Desktop.getDesktop();
            URI url = new URI(requestUrl);
            desktop.browse(url);

            String token = oauthHelper.getAccessToken(oauthParameters);
    }

【问题讨论】:

  • 您为这些选项中的每一个使用了哪些redirect_uri 值?您是否使用相同的流程?
  • 我将如何使用 Google Documents List API 进行身份验证的代码添加到 OP。

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


【解决方案1】:

命令行示例是为了简单而编写的,不一定是最好的用户体验。在这种情况下,它们作为本地应用程序运行,并使用 OAuth 2.0 的已安装应用程序流。该流程确实有一种模式,redirect_uri 可以指向 localhost,但它需要启动一个临时 Web 服务器来接收重定向。它没有使示例复杂化,而是使用需要复制/粘贴代码的 OOB 模式。

如果您正在构建桌面应用程序,我建议您采用重定向到 localhost 的路线,因为它是更好的用户体验。

请参阅https://developers.google.com/accounts/docs/OAuth2InstalledApp 了解更多信息。

【讨论】:

  • 谢谢!这正是我想要的!
  • 好的,但是如何在不调用新授权的情况下使用存储的刷新令牌获取新的访问令牌?它没有在java中指定..
【解决方案2】:

将您的redirect_uri 更改为您的本地主机页面或项目页面。所提供链接处的请求将发送您的代码。请求将在其 url 中包含 code="yourauthcode"。例子: https://yourwebsite.com/yourpage.htm?code="你的验证码"

【讨论】:

    【解决方案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 以处理错误和刷新令牌

    【讨论】:

    • 我们从哪里得到code(如步骤2中的flow.newTokenRequest(code)?)
    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2020-05-27
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    相关资源
    最近更新 更多