简体   繁体   English

Google登录-尝试访问userinfo时出现401错误“需要登录”

[英]Google Login - 401 error “Login Required” when trying to access userinfo

I have an Android app that supports Login with Google using PlusClient . 我有一个Android应用程序,支持使用PlusClient与Google登录。 After I connect, I get the auth token using an AsyncTask and the GoogleAuthUtil.getToken method as follows: 连接后,使用AsyncTaskGoogleAuthUtil.getToken方法获取身份验证令牌,如下所示:

private void fetchAuthToken() { AsyncTask task = new AsyncTask() { 私人无效fetchAuthToken(){AsyncTask任务=新的AsyncTask(){

        @Override
        protected String doInBackground(Void... params) {
            String token = null;
            String scope = "oauth2: https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email";//"oauth2: "+ Scopes.PLUS_PROFILE;
            Bundle appActivities = new Bundle();
            appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES, visiblaeActivities);
            try {                   
                token = GoogleAuthUtil.getToken(Login.instance.context, mPlusClient.getAccountName(), scope, appActivities);
            } catch (UserRecoverableAuthException e) {                  
                loginActivity.startActivityForResult(e.getIntent(), REQUSET_TOKEN_PERMISSIONS_RESLOVE_ERR);                 
            } catch (IOException e) {
                e.printStackTrace();
            } catch (GoogleAuthException e) {
                e.printStackTrace();
            }
            return token;
        }

This works great, and then I send the token to my backend which is in node.js, there, I try to fetch the user details from google using this request: 这很好用,然后我将令牌发送到node.js中的后端,在那里,我尝试使用此请求从google获取用户详细信息:

https://www.googleapis.com/oauth2/v2/userinfo?access_token=' + access_token

and then I get the user's details and everything is Hunky Dory. 然后我得到了用户的详细信息,一切都是Hunky Dory。 The problem is, sometimes I get this error that the token is not valid, which looks like this: 问题是,有时我会收到以下错误消息:令牌无效,看起来像这样:

"error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } } “错误”:{“错误”:[{“域”:“全局”,“原因”:“必填”,“消息”:“需要登录”,“ locationType”:“页眉”,“位置”:“授权“}],” code“:401,” message“:”需要登录“}}

And my guess is someone tries to login with google but can't connect for some reason. 我的猜测是有人试图登录google但由于某种原因而无法连接。 this is hard to debug because it happens sporadically, and most users login successfully. 这很难调试,因为它偶尔会发生,并且大多数用户都可以成功登录。 Anyone? 任何人?

PS I only fetch the token after the user is connected, ie - I call fetchAuthToken() within the onConnected callback, so the user is definitely logged in by then PS我仅在用户连接后才获取令牌,即-我在onConnected回调中调用fetchAuthToken() ,因此到那时用户肯定已登录

My idea: are you keeping your session info? 我的想法:您要保留会话信息吗? Your http client needs to keep track of the session otherwise the token might become invalid. 您的http客户端需要跟踪会话,否则令牌可能变得无效。

BasicCookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

and then add context to your post/get/whatever request you are making 然后将上下文添加到您发出的帖子/获取/任何请求中

client.execute(post, localContext);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM