使用OAuth2以编程方式向Google进行身份验证 [英] Authenticate programmatically to Google with OAuth2

查看:170
本文介绍了使用OAuth2以编程方式向Google进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以编程方式向Google进行身份验证?
现在ClientLogin( https://developers.google.com/accounts/docs/AuthForInstalledApps
已弃用,我们如何使用OAuth2对Google执行程序化身份验证?



使用ClientLogin,我们可以执行帖子
https://www.google.com/accounts/ClientLogin
with email and密码参数并获取身份验证令牌。



使用OAuth2我找不到解决方案!





我的应用程序是一个java后台进程。
我看到,点击此链接:developers.google.com/accounts/docs/OAuth2InstalledApp#refresh,如何使用刷新的令牌获取新的访问令牌。



<问题是,当我有一个新的有效访问令牌时,我找不到关于如何实例化Analytics对象(例如)来执行查询的java示例



这是我的代码在调用execute()时返回401无效凭据:

  public class Test {

static final String client_id =MY_CLIENT_ID;
static final String client_secret =MY_SECRET;
static final String appName =MY_APP;

private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();

static String access_token =xxxx;
static String refreshToken =yyyyy;

public static void main(String args []){

try {

GoogleCredential credential =
new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setClientSecrets(client_id,client_secret).build();
credential.setAccessToken(access_token);
credential.setRefreshToken(refreshToken);
// GoogleCredential
Analytics analytics = Analytics.builder(HTTP_TRANSPORT,JSON_FACTORY)
.setApplicationName(appName)
.setHttpRequestInitializer(credential)
.build();

账户账户= analytics.management()。accounts()。list()。execute();
} catch(例外e){
e.printStackTrace();
}
}

有什么问题?

解决方案

检查已安装应用程序的OAuth 2流程:



https://developers.google.com/accounts/docs/OAuth2InstalledApp



它仍然要求用户第一次使用浏览器进行身份验证,但随后您可以存储刷新令牌并将其用于后续请求。



对于其他解决方案,请检查设备流程或服务帐户,它们在同一文档集中进行了解释。


How can I authenticate programmatically to Google? Now that ClientLogin (https://developers.google.com/accounts/docs/AuthForInstalledApps) is deprecated, how can we perform a programmatic authentication to Google with OAuth2?

With ClientLogin we could perform a post to https://www.google.com/accounts/ClientLogin with email and password parameters and obtain the authentication token.

With OAuth2 i can't find a solution!

#

My app is a java background process. I saw, following this link: developers.google.com/accounts/docs/OAuth2InstalledApp#refresh, how to obtain a new access token using a refreshed token.

The problem is that I can't find a java example about how to instantiate an Analytics object (for example) to perform a query when I have a new valid access token

This is my code that returns a 401 Invalid credentials when invoke the "execute()":

public class Test {

static final String client_id = "MY_CLIENT_ID";
static final String client_secret = "MY_SECRET";
static final String appName = "MY_APP";

private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();

static String access_token = "xxxx";
static String refreshToken = "yyyyy";

public static void main (String args[]){

    try {

        GoogleCredential credential = 
            new GoogleCredential.Builder()
                .setTransport(HTTP_TRANSPORT)
                .setJsonFactory(JSON_FACTORY)
                .setClientSecrets(client_id, client_secret).build();
        credential.setAccessToken(access_token);
        credential.setRefreshToken(refreshToken);
        //GoogleCredential
        Analytics analytics = Analytics.builder(HTTP_TRANSPORT, JSON_FACTORY)
            .setApplicationName(appName)
            .setHttpRequestInitializer(credential)
            .build();

        Accounts accounts = analytics.management().accounts().list().execute();
    } catch (Exception e) {
        e.printStackTrace();
    } 
}

What is the problem?

解决方案

Check the OAuth 2 flow for Installed Application:

https://developers.google.com/accounts/docs/OAuth2InstalledApp

It still requires the user to authenticate with a browser the first time, but then you can store the refresh token and use it for subsequent requests.

For alternative solutions, check the Device flow or Service Accounts, they are explained in the same documentation set.

这篇关于使用OAuth2以编程方式向Google进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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