401当我的javascript应用程序尝试访问我的google云端点时出错

2024-05-18 22:29:06 发布

您现在位置:Python中文网/ 问答频道 /正文

这个项目是在googleappenginecloudendpoints框架上进行的。 后端的Python。 我还使用endpoints\u proto\u datastore(不确定这是否有区别)

这是我的html文件-

<html>
<body>

<script>

clientId = 'myclientid-something-something-something.apps.googleusercontent.com'
loginScope = 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/admin.directory.user.readonly https://www.googleapis.com/auth/admin.directory.customer.readonly https://www.googleapis.com/auth/gmail.settings.basic';        
apiKey = 'my-api-key-from-cloud-console';

doSignIn = function() {
  console.log("calling doSignIn");
  gapi.client.init({apiKey: apiKey, clientId: clientId,scope: loginScope}).then(renderSignIn);
}

renderSignIn = function(){
              gapi.signin2.render('my-signin', {
                'scope': loginScope,
                'width': 'inherit',
                'height': 50,
                'longtitle': true,
                'theme': 'light',
                'onsuccess': getOfflineAccess,
                'onfailure': function(){console.log("error")}
             });
};

getOfflineAccess =function(){
  console.log("calling getOfflineAccess");
  gapi.auth2.getAuthInstance().grantOfflineAccess({'redirect_uri': 'postmessage'}).then(getApiAuthorization);
}
getApiAuthorization = function(){
  console.log("calling getApiAuthorization");
  gapi.auth.authorize({client_id: clientId,scope: loginScope, immediate: false},singedInCallback);
};
singedInCallback = function(authResponse) {
    console.log("calling signInCallback");
    gapi.client.endpointsapp.userOfflineAccessCode.insert({'auth_code':authResponse.code})
           .then(   function(){console.log("success");},  
                    function(){console.log("error");}
                ); 
};



init = function() {
  console.log("calling init");
  var apisToLoad;
  var callback = function() {
    if (--apisToLoad == 0) {
      doSignIn();
    }
  }
  apisToLoad = 2; 
  gapi.client.load('endpointsapp','v1',callback,"https://endpointsapp.appspot.com/_ah/api"); //dummy name for app
  gapi.load('client:auth2', callback);
};

</script>
<div id="my-signin"></div>

    <script src="https://apis.google.com/js/api.js?onload=init"></script>  
    <script src="https://apis.google.com/js/client.js?onload=init"></script>  
    <script src="https://apis.google.com/js/platform.js"></script>

</body>
</html>

一开始一切都很顺利。 我得到一个谷歌签名按钮。 我单击它,然后授予所有必需的权限。 当实际的API命中时。这给了我401。你知道吗

我从API得到的响应(gapi.client.endpointsapp美国serOfflineAccessCode.insert文件)是:

{
 "error": {
  "code": 401, 
  "errors": [
   {
    "domain": "global", 
    "message": "Invalid token.", 
    "reason": "required"
   }
  ], 
  "message": "Invalid token."
 }
}

当我使用googleapi浏览器尝试相同的api端点时,如果我通过了身份验证,一切都正常,没有任何问题。你知道吗

我已经试着调试了一整天,但就是不知道我做错了什么。你知道吗

非常感谢您的帮助。你知道吗


Tags: httpscomclientauthloginitwwwjs
1条回答
网友
1楼 · 发布于 2024-05-18 22:29:06

好的,找到问题了。非常基本的错误。你知道吗

根据https://cloud.google.com/endpoints/docs/frameworks/python/create_api,如果API使用身份验证,那么允许的客户机id是必填字段。我没有提供这个参数,并且希望API在默认情况下对所有客户端ID都可用。你知道吗

相关问题 更多 >

    热门问题