“Uncaught TypeError: Cannot read property 'people' of undefined ”

“Uncaught TypeError: Cannot read property 'people' of undefined ”

我正在尝试使用 Plus API 登录用户。我在控制台中收到以下错误:

未捕获类型错误:无法读取未定义的 属性 'people'

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>

虽然 gapi.client.load 有第三个参数可选,但是人们在将它与 promises ref 一起使用时观察到错误:Undefined Error with Gapi.load

尝试传递回调函数,而不是将其用作承诺。

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);
            });
        });
    }

问题已解决。 这是一个错误的 API 键。

脚本没有加载所以 我以为这是一个错误 API 正确假设的关键