我正试图在我的网站上添加一个谷歌登录按钮,但由于某种原因,我遇到了一个错误,我不知道如何修复它。我查看了错误消息中的链接,但仍然无法修复。我也研究了其他相关的主题,但没有发现任何有助于解决我的问题的东西。
错误
code: 401, message: "Request had invalid authentication credentials. Expected OAuth 2 access
token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
status: "UNAUTHENTICATED"
执行此代码时im收到错误消息
gapi.client.people.people.get({'resourceName': 'people/me', 'personFields': 'names,emailAddresses'}).then(function (resp) {
我的代码:
const apiKey = XXXXXXX;
const discoveryDocs = ["https://people.googleapis.com/$discovery/rest?version=v1"];
const clientId = 'XXXXXX-XXXXXX.apps.googleusercontent.com';
const scopes = 'profile';
const authorizeButton = document.getElementById('btn-google');
if(authorizeButton){
authorizeButton.addEventListener('click', () => {
gapi.load('client:auth2', initClient);
})
}
function initClient() {
gapi.client.init({
apiKey: apiKey,
discoveryDocs: discoveryDocs,
clientId: clientId,
scope: scopes,
plugin_name: "chat"})
.then(function () {
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
authorizeButton.onclick = handleAuthClick;
});
}
function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
makeApiCall();
}
}
function handleAuthClick(event) {
gapi.auth2.getAuthInstance().signIn();
}
function makeApiCall() {
gapi.client.people.people.get({'resourceName': 'people/me', 'personFields': 'names,emailAddresses'}).then(function (resp) {
try{
const formData = new FormData();
formData.append('email', resp.result.emailAddresses[0].value);
formData.append('firstname', resp.result.names[0].givenName);
formData.append('lastname', resp.result.names[0].familyName);
fetch('index.php?route=extension/module/google_login/login', {
method: 'POST',
body: formData,
})
.then(response => response.json())
.then(json => {
if(json.success == true){
updateHeader();
}
})
}
});
}