我正在尝试使用Plus API来登录用户。我在控制台中得到以下错误:
Uncaught TypeError: Cannot read property 'people' of undefined
url为:http://fbconnect.yudazdk.com/google_connect.php
我的代码是:<!DOCTYPE html>
<html>
<head>
<title>Google Connect JavaScript test</title>
<meta charset="UTF-8">
</head>
<body>
<h2>Google Connect Test Page</h2>
<button id="authorize-button" style="visibility: hidden">Authorize</button>
<script type="text/javascript">
<!--Add a button for the user to click to initiate auth sequence -->
var clientId = 'xxxxx-09ecq1a33q91tvsd50rd2g5n0qiuortd.apps.googleusercontent.com';
var apiKey = 'xxxxxx';
var scopes = 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me';
scopes += ' https://www.googleapis.com/auth/userinfo.email';
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: true}, handleAuthResult);
}
function handleAuthResult(authResult) {
var authorizeButton = document.getElementById('authorize-button');
if (authResult && !authResult.error) {
console.log('auth result');
console.log(authResult);
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;
}
function makeApiCall() {
gapi.client.load('plus','v1', function() {
var request = gapi.client.plus.people.get( {
'userId': 'me'
});
request.execute(function(resp) {
console.log('Retrieved profile for:');
console.log(resp);
});
});
}
// Load the API and make an API call. Display the results on the screen.
function makeApiCall1() {
// 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>
</body>
</html>
问题解决。这是一个错误的API密钥。
脚本没有加载,所以我认为这是一个错误的API关键是什么是正确的假设
虽然gapi.client.load
有第三个参数是可选的,但是人们在使用它与promises一起使用时发现了错误
尝试传递回调函数,而不是将其用作承诺。
function makeApiCall1() {
gapi.client.load('plus', 'v1',function() {
var request = gapi.client.plus.people.get({
'userId': 'me'
});
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);
});
});
}