Google Maps Tracks API 401无效的凭证 [英] Google Maps Tracks API 401 Invalid Credentials

查看:154
本文介绍了Google Maps Tracks API 401无效的凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 https://www.googleapis上检查了我的access_token的有效性.com / oauth2 / v1 / tokeninfo?access_token = mytoken
正在回复如下,

I do have checked validity of my access_token at https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=mytoken which is replying as follows,

{
 "issued_to": "myservice_account_emailid",
 "audience": "myservice_account_emailid",
 "scope": "https://www.googleapis.com/auth/tracks",
 "expires_in": 1300,
 "access_type": "offline"
}

这意味着我的令牌仍然有效,但如果我向谷歌跟踪api的请求,使用相同的access_token,我会得到如下响应,

which means still my token is valid, but if i make request to google tracks api from my application, using the same access_token, am getting response as below ,

Response: {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

Google开发人员控制台中的新项目,并创建了新的服务帐户,并在应用程序中使用了新凭据,但仍得到相同的响应。可能是什么原因?

Even i created new project in google developer console, and created new service account , and used the new credentials in application, but still getting same response. what could be the reason?

推荐答案

找到我的自我。

我的错误代码如下,

var jwt = new googleapis.auth.JWT(client_email, keyFile, null, scopes, null);

jwt.authorize(function(jwtErr, tokens){
    if(jwtErr){
        return;
    }
    else{
        headers = {
            'content-type' : 'application/json;charset=utf-8',
            'content-length': Buffer.byteLength(data),
            'Authorization' : tokens.access_token
        };

        options = {
            host: 'www.googleapis.com',
            path: '/tracks/v1/entities/create',
            headers : headers,
            method : 'POST',
        };

        var post_req = https.request(options, function(res){
              res.setEncoding('utf8');
              res.on('data', function (chunk) {
                  console.log('Response: ' + chunk);
              });
        });
        post_req.on('error', function(error_msg){
            console.log(error_msg);
        });
        post_req.write(data);
        post_req.end();
    }
});

问题出在header对象中,'Authorization'字段被设置为只有access.token的值我们还必须指定token_type值。
,因此标题必须如下所示。

The problem is in header object, 'Authorization' field is set to only access.token value, along with that we must specify token_type value also. so header must be like below.

headers = {
                'content-type' : 'application/json;charset=utf-8',
                'content-length': Buffer.byteLength(data),
                'Authorization' : tokens.token_type+' '+tokens.access_token
            };

现在它解决了401无效凭证错误。

and now it solves 401 Invalid Credentials error.

这篇关于Google Maps Tracks API 401无效的凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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