【发布时间】:2018-02-16 14:56:38
【问题描述】:
我正在尝试访问 Google People API 以为我的 Google App Engine 应用提供身份验证。
我收到关于空引用者的错误消息,但我在云控制台中设置了我的 HTTP 引用者
{
"error": {
"code": 403,
"message": "Requests from referer \u003cempty\u003e are blocked.",
"status": "PERMISSION_DENIED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.Help",
"links": [
{
"description": "Google developer console API key",
"url": "https://console.developers.google.com/project/824515690907/apiui/credential"
}
]
}
]
}
}
这是我的gapi.js 文件:
var apiKey = '<redacted>';
var discoveryDocs = ["https://people.googleapis.com/$discovery/rest?version=v1"];
var clientId = 'my-client-id.apps.googleusercontent.com';
var scopes = 'profile';
var authorizeButton = document.getElementById('authorize-button');
var signoutButton = document.getElementById('signout-button');
var mainDiv = document.getElementById('main');
var editNav = document.getElementById('edit');
authorizeButton.addEventListener("click", function(){
handleAuthClick();
});
signoutButton.addEventListener("click", function(){
handleSignoutClick();
});
function handleClientLoad() {
// Load the API client and auth2 library
gapi.load('client:auth2', initClient);
}
function start() {
gapi.client.init({
'apiKey': apiKey,
// clientId and scope are optional if auth is not required.
'clientId': clientId,
'scope': 'profile',
}).then(function() {
return gapi.client.request({
'path': 'https://people.googleapis.com/v1/people/me?requestMask.includeField=person.names,person.emailAddresses',
'headers': {'Content-Type': 'application/json','Referer': 'https://<my-app>.appspot.com/*'}
})
}).then(function(response) {
console.log(response.result);
updateSigninStatus(response);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
authorizeButton.style.display = 'inline-block';
});
};
gapi.load('client', start);
mainDiv.style.display = 'none';
/*functions*/
function updateSigninStatus(response) {
var name = response.result.names[0].givenName;
var email = response.result.emailAddresses[0].value;
authorizeButton.insertAdjacentHTML('beforebegin', '<span id="loggedinuser" rel="' + email + '">Logged in as ' + name + '</span>');
authorizeButton.style.display = 'inline-block';
}
}
function handleAuthClick(event) {
gapi.auth2.getAuthInstance().signIn();
location.reload();
}
function handleSignoutClick(event) {
gapi.auth2.getAuthInstance().signOut();
var loggedin = document.getElementById("loggedinuser");
loggedin.parentNode.removeChild(loggedin);
var userStatus = document.getElementById("user_status");
userStatus.parentNode.removeChild(userStatus);
location.reload();
}
我阅读了另一个关于将引用者作为参数放入请求中的问题和答案,但我不知道该放在哪里。
谁能看到我的代码有什么问题?我有一个早期版本工作了一段时间,然后就出错了。
有谁知道 Google API 请求脚本的最新示例(Google 提供的 GitHub 上的那些不工作)。
更新
刚刚检查了网络标签中的标题
Request URL:https://content-people.googleapis.com/v1/people/me?requestMask.includeField=person.names,person.emailAddresses&alt=json&key=<myApiKey>
Request Method:GET
Status Code:401
Remote Address:216.58.204.74:443
Referrer Policy:no-referrer-when-downgrade
根据this answer on superuser about referrers 和this answer on SO about a 403 error on a Google Maps API request。
【问题讨论】:
-
gapi.js是从浏览器运行的吗?我认为浏览器不允许您编辑引荐来源网址 -
是的,它正在从浏览器运行 - 另一个 StackOverflow 问答建议编辑引荐来源网址(但引荐来源网址实际上是在 Google 控制台中指定的),还有另一个 StackOverflow 问答建议在JS.