繁体   English   中英

Google Maps Tracks API 401无效的凭据

[英]Google Maps Tracks API 401 Invalid Credentials

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

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

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

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

甚至我在Google Developer Console中创建了新项目,并创建了新的服务帐户,并在应用程序中使用了新的凭据,但仍然得到了相同的响应。 可能是什么原因?

找到了我的自我。

我的容易出错的代码如下,

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

问题出在标题对象中,“ Authorization”字段设置为仅access.token值,我们还必须指定token_type值。 因此标题必须如下所示。

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

现在它解决了401 Invalid Credentials错误。

暂无
暂无

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

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