当我的 javascript 应用程序尝试访问我的 google 云端点时出现 401 错误
401 error when my javascript app tries to access my google cloud endpoints
该项目在 Google Appengine 云端点框架上。
Python 在后端。
我也在使用 endpoints_proto_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>
一开始一切都很顺利。
我得到一个 google 签名按钮。
我单击它,然后授予所有必需的权限。
当实际 API 命中时。这给了我一个 401。
我从 API (gapi.client.endpointsapp.userOfflineAccessCode.insert) 得到的响应是:
{
"error": {
"code": 401,
"errors": [
{
"domain": "global",
"message": "Invalid token.",
"reason": "required"
}
],
"message": "Invalid token."
}
}
当我使用 google api 浏览器尝试相同的 api 端点时,如果我通过身份验证,一切正常,没有任何问题。
我一整天都在尝试对此进行调试,但就是无法弄清楚我做错了什么。
非常感谢任何帮助。
好的,找到问题了。非常基本的错误。
根据 https://cloud.google.com/endpoints/docs/frameworks/python/create_api,如果 API 使用身份验证,则 allowed_client_ids 是必填字段。我没有提供此参数并期望默认情况下 API 对所有 client_ids 可用。
该项目在 Google Appengine 云端点框架上。 Python 在后端。 我也在使用 endpoints_proto_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>
一开始一切都很顺利。 我得到一个 google 签名按钮。 我单击它,然后授予所有必需的权限。 当实际 API 命中时。这给了我一个 401。
我从 API (gapi.client.endpointsapp.userOfflineAccessCode.insert) 得到的响应是:
{
"error": {
"code": 401,
"errors": [
{
"domain": "global",
"message": "Invalid token.",
"reason": "required"
}
],
"message": "Invalid token."
}
}
当我使用 google api 浏览器尝试相同的 api 端点时,如果我通过身份验证,一切正常,没有任何问题。
我一整天都在尝试对此进行调试,但就是无法弄清楚我做错了什么。
非常感谢任何帮助。
好的,找到问题了。非常基本的错误。
根据 https://cloud.google.com/endpoints/docs/frameworks/python/create_api,如果 API 使用身份验证,则 allowed_client_ids 是必填字段。我没有提供此参数并期望默认情况下 API 对所有 client_ids 可用。