“未捕获的TypeError:无法读取属性' people'未定义的“

时间:2015-09-17 02:41:19

标签: javascript google-api google-login

我正在尝试使用Plus API对用户进行签名。我在控制台中收到以下错误:

未捕获的TypeError:无法读取属性' people'未定义的

网址为: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>

1 个答案:

答案 0 :(得分:0)

虽然gapi.client.load的第三个参数是可选的,但是人们在使用promises参考时发现了错误: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);
            });
        });
    }