11

我正在使用 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();
}

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

3 回答 3

7

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

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

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

于 2012-09-21T00:57:05.190 回答
6

第 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:03:27.270 回答
0

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

于 2013-06-20T12:04:56.433 回答