gpt4 book ai didi

google-people-api - Google People API 空引荐来源网址(但引荐来源网址是在控制台中设置的)

转载 作者:行者123 更新时间:2023-12-02 17:40:13 30 4
gpt4 key购买 nike

我正在尝试访问 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 上的示例不起作用)。

更新

刚刚检查了网络选项卡中的 header

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 referrersthis answer on SO about a 403 error on a Google Maps API request

最佳答案

gapi.js

  var apiKey = '<redacted>';
var discoveryDocs = ["https://people.googleapis.com/$discovery/rest?version=v1"];

var clientId = '<redacted>.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');
function handleClientLoad() {
// Load the API client and auth2 library
gapi.load('client:auth2', initClient);
mainDiv.style.display = 'none';
}
function initClient() {
gapi.client.init({
apiKey: apiKey,
discoveryDocs: discoveryDocs,
clientId: clientId,
scope: scopes
}).then(function () {
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick;
signoutButton.onclick = handleSignoutClick;
});
}
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
authorizeButton.style.display = 'none';
signoutButton.style.display = 'block';
makeApiCall();
} else {
authorizeButton.style.display = 'block';
signoutButton.style.display = 'none';
}
}
function handleAuthClick(event) {
gapi.auth2.getAuthInstance().signIn();
}
function handleSignoutClick(event) {
gapi.auth2.getAuthInstance().signOut();
}
// Load the API and make an API call. Display the results on the screen.
function makeApiCall() {
gapi.client.people.people.get({
'resourceName': 'people/me',
'requestMask.includeField': 'person.names,person.emailAddresses'
}).then(function(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>');
}
});
}

index.html

<script src="js/gapi.js"></script>
<script async defer src="https://apis.google.com/js/api.js"
onload="this.onload=function(){};handleClientLoad()"
onreadystatechange="if (this.readyState === 'complete') this.onload()">
</script>

改编自:https://github.com/google/google-api-javascript-client/blob/master/samples/authSample.html

关于google-people-api - Google People API 空引荐来源网址(但引荐来源网址是在控制台中设置的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48829368/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com