如何在不要求用户复制/粘贴身份验证代码的情况下对 Google 云端硬盘进行身份验证



我正在使用DriveCommandLine应用程序来学习Drive API。 我只是想知道是否可以使用 Google 云端硬盘对我的桌面应用程序进行身份验证,而无需用户从浏览器复制/粘贴身份验证代码? 而是只是将令牌从浏览器传递回应用程序? 我可以使用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();
}

谷歌文档列表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);
    }

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

如果您正在构建桌面应用程序,我鼓励您选择重定向到 localhost,因为它是一个更好的用户体验。

有关详细信息,请参阅 https://developers.google.com/accounts/docs/OAuth2InstalledApp。

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

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:存储凭据访问令牌和刷新令牌

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

将redirect_uri更改为本地主机页面或项目页面。通过提供的链接请求将发送您的代码。请求的网址中将包含 code="yourauthcode"。例:https://yourwebsite.com/yourpage.htm?code="您的代码"

相关内容

最新更新