如何无需用户进行复制/粘贴AUTH code验证谷歌驱动器? [英] How to authenticate Google Drive without requiring the user to copy/paste auth code?

查看:423
本文介绍了如何无需用户进行复制/粘贴AUTH code验证谷歌驱动器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩与周围的DriveCommandLine应用程序了学习驱动API一点。我只是想知道如果有可能无需用户复制到验证自己与谷歌驱动器的桌面应用程序/粘贴浏览器的auth code?而只是传递回从浏览器应用程序令牌?我能与Dropbox的API,并与谷歌文档列表API要做到这一点,但无法弄清楚如何得到这个工作与谷歌云端硬盘API。

感谢。

谷歌云端硬盘API - DriveCommandLine示例应用程序(略有修改):

 公共类DriveCommandLine {  私人静态字符串CLIENT_ID = APPCONSTANTS.Google.CONSUMER_KEY;
  私人静态字符串CLIENT_SECRET = APPCONSTANTS.Google.CONSUMER_SECRET;  私人静态字符串REDIRECT_URI =金塔:IETF:WG:OAuth的:2.0:OOB  公共静态无效的主要(字串[] args)抛出IOException异常,{的URISyntaxException
    HttpTransport httpTransport =新NetHttpTransport();
    JsonFactory jsonFactory =新JacksonFactory();    GoogleAuthorization codeFLOW流量=新GoogleAuthorization codeFlow.Builder(
        httpTransport,jsonFactory,CLIENT_ID,CLIENT_SECRET,Arrays.asList(DriveScopes.DRIVE))
        .setAccessType(离线)
        .setApprovalPrompt(力)建设()。    字符串URL = flow.newAuthorizationUrl()setRedirectUri(REDIRECT_URI).build();
    的System.out.println(输入授权code:);
    。Desktop.getDesktop()浏览(新的URI(URL));
    BR的BufferedReader =新的BufferedReader(新的InputStreamReader(System.in));
    字符串code = br.readLine();    GoogleTokenResponse响应= flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
    GoogleCredential证书=新GoogleCredential()setFromTokenResponse(响应)。    //创建一个新的授权API客户端
    驱动器的服务=新Drive.Builder(httpTransport,jsonFactory,证书).build();
}

谷歌文档列表API:

 公共无效的身份验证(){
            GoogleOAuthParameters oauthParameters =新GoogleOAuthParameters();
            oauthParameters.setOAuthConsumerKey(APPCONSTANTS.Google.CONSUMER_KEY);            OAuthSigner签名;
            如果(APPCONSTANTS.Google.USE_RSA_SIGNING){
                    签名者=新OAuthRsaSha1Signer(APPCONSTANTS.Google.CONSUMER_SECRET);
            }其他{
                oauthParameters.setOAuthConsumerSecret(APPCONSTANTS.Google.CONSUMER_SECRET);
                签名者=新OAuthHmacSha1Signer();
            }            GoogleOAuthHelper oauthHelper =新GoogleOAuthHelper(签名者);            oauthParameters.setScope(APPCONSTANTS.Google.SCOPES);            oauthHelper.getUnauthorizedRequestToken(oauthParameters);            字符串requestUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters);            桌面桌面= Desktop.getDesktop();
            URI URL =新的URI(requestUrl);
            desktop.browse(URL);            字符串标记= oauthHelper.getAccessToken(oauthParameters);
    }


解决方案

命令行样品为简单起见写的,不一定是最好的用户体验。在这种情况下,他们正在运行的应用程序的本地和使用,OAuth 2.0的安装的应用程序流量。该流也有一个模式,其中REDIRECT_URI可以指向本地主机,但它需要启动一个临时网络服务器接收该重定向。而不是样品复杂的是,它使用了需要复制/粘贴code中的OOB模式。

如果你正在构建一个桌面应用程序,我会鼓励去重定向到本地主机,因为它是一个更好的UX的路线。

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

I'm playing around with the DriveCommandLine applicaiton to learn the Drive API a bit. I'm just wondering if it is possible to authenticate my desktop application with Google Drive without the user needing to copy/paste an auth code from the browser? But rather just have a token passed back to the app from the browser? I am able to do this with Dropbox API and with the Google Documents List API, but cannot figure out how to get this working with the Google Drive API.

Thanks.

Google Drive API - DriveCommandLine sample app (slightly modified):

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

解决方案

The command line samples were written for simplicity, not necessarily the best user experience. In this case, they're running as local apps and are using the installed app flow for OAuth 2.0. That flow does have a mode where the redirect_uri can point to localhost, but it requires starting up a temporary web server to receive the redirect. Rather than complicate the sample, it uses the OOB mode which requires copy/pasting the code.

If you're building a desktop app, I'd encourage going the route of redirecting to localhost as it is a better UX.

See https://developers.google.com/accounts/docs/OAuth2InstalledApp for more info.

这篇关于如何无需用户进行复制/粘贴AUTH code验证谷歌驱动器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆