我正在尝试连接 Google 客户端 API 以在我的网站上显示 Google Analytics 数据。
所以我使用本教程尝试连接客户端 API: https ://developers.google.com/api-client-library/javascript/start/start-js
我按照所有说明操作,在 Google 开发者控制台上创建了一个 OAuth 客户端。
这是我的代码:
<button id="authorize-button">Authorize</button>
<script type="text/javascript">
var clientId = 'XXXXXXXXX';
var apiKey = 'XXXXXXX';
var scopes = 'https://www.googleapis.com/auth/plus.me';
function handleClientLoad() {
// Step 2: Reference the API key
gapi.client.setApiKey(apiKey);
window.setTimeout(checkAuth,1);
}
function checkAuth() {
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
}
function handleAuthResult(authResult) {
var authorizeButton = document.getElementById('authorize-button');
if (authResult && !authResult.error) {
authorizeButton.style.visibility = 'hidden';
makeApiCall();
} else {
authorizeButton.style.visibility = '';
authorizeButton.onclick = handleAuthClick;
}
}
function handleAuthClick(event) {
// Step 3: get authorization to use private data
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
return false;
}
// Load the API and make an API call. Display the results on the screen.
function makeApiCall() {
// Step 4: Load the Google+ API
gapi.client.load('plus', 'v1').then(function() {
// Step 5: Assemble the API request
var request = gapi.client.plus.people.get({
'userId': 'me'
});
// Step 6: Execute the API request
request.then(function(resp) {
var heading = document.createElement('h4');
var image = document.createElement('img');
image.src = resp.result.image.url;
heading.appendChild(image);
heading.appendChild(document.createTextNode(resp.result.displayName));
document.getElementById('content').appendChild(heading);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
});
}
</script>
// Step 1: Load JavaScript client library
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
但它不起作用,我不知道是什么问题,我得到这个错误:
401. That’s an error.
Error: invalid_client
The OAuth client was not found.
Request Details
immediate=false
response_type=token
scope=https://www.googleapis.com/auth/plus.me
redirect_uri=postmessage
proxy=oauth2relay509775948
state=658721539|0.1255556678
origin=http://localhost
include_granted_scopes=true
client_id='MY SECRED KEY'
authuser=0
That’s all we know.
如果有人有解决方案,那应该非常好;)在此先感谢。