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

时间:2012-09-20 21:44:26

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

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

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);
    }

3 个答案:

答案 0 :(得分:6)

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

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

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

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

答案 2 :(得分:0)

将redirect_uri更改为localhost页面或项目页面。提供的链接上的请求将发送您的代码。请求将在其网址中包含code =“yourauthcode”。例: https://yourwebsite.com/yourpage.htm?code= “yourauthcode”