gpt4 book ai didi

javascript - 尝试获取配置文件数据时出现 API key 无效错误

转载 作者:行者123 更新时间:2023-12-03 04:31:46 24 4
gpt4 key购买 nike

我正在尝试测试 Google 登录 API。我想在客户端登录后检索一些基本数据。

我创建了一个 Google API 控制台项目和客户端 ID ( https://developers.google.com/identity/sign-in/web/devconsole-project )

这是谷歌官方代码(https://developers.google.com/api-client-library/javascript/samples/samples#LoadinganAPIandMakingaRequest):

<html>
<head>
</head>
<body>
<script type="text/javascript">
function handleClientLoad() {
// Loads the client library and the auth2 library together for efficiency.
// Loading the auth2 library is optional here since `gapi.client.init` function will load
// it if not already loaded. Loading it upfront can save one network request.
gapi.load('client:auth2', initClient);
}

function initClient() {
// Initialize the client with API key and People API, and initialize OAuth with an
// OAuth 2.0 client ID and scopes (space delimited string) to request access.
gapi.client.init({
apiKey: '5v2RzP7-xyQGNjxrD5suoPL9',
discoveryDocs: ["https://people.googleapis.com/$discovery/rest?version=v1"],
clientId: '298062822261-e5c09q8191mkho0o7n3n3obiq2eq2p3f.apps.googleusercontent.com',
scope: 'profile'
}).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());
});
}

function updateSigninStatus(isSignedIn) {
// When signin status changes, this function is called.
// If the signin status is changed to signedIn, we make an API call.
if (isSignedIn) {
makeApiCall();
}
}

function handleSignInClick(event) {
// Ideally the button should only show up after gapi.client.init finishes, so that this
// handler won't be called before OAuth is initialized.
gapi.auth2.getAuthInstance().signIn();
}

function handleSignOutClick(event) {
gapi.auth2.getAuthInstance().signOut();
}

function makeApiCall() {
// Make an API call to the People API, and print the user's given name.
gapi.client.people.people.get({
resourceName: 'people/me'
}).then(function(response) {
console.log('Hello, ' + response.result.names[0].givenName);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
}
</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>
<button id="signin-button" onclick="handleSignInClick()">Sign In</button>
<button id="signout-button" onclick="handleSignOutClick()">Sign Out</button>
</body>
</html>

ClientId 和 apikey 来 self 的 Google api。

我收到错误 400

{
"error": {
"code": 400,
"message": "API key not valid. Please pass a valid API key.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.Help",
"links": [
{
"description": "Google developers console",
"url": "https://console.developers.google.com"
}
]
}
]
}
}

我不明白我的 api key 出了什么问题。我按照 Google 文档中的所有步骤进行操作。

最佳答案

您似乎正在使用客户端 key 作为 API key 。不是吗?

要获取 API key ,您必须创建它。

在凭据(开发控制台)下,单击“创建凭据”,然后单击“API key ”

关于javascript - 尝试获取配置文件数据时出现 API key 无效错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43456740/

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