2015-09-17 54 views
0

我试图用加API登录用户我在控制台中出现以下错误:“遗漏的类型错误:无法读取属性‘人’的未定义”

遗漏的类型错误:无法读取属性“未定义

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> 

回答

1

问题解决了。 这是一个错误的API密钥。

未加载脚本,以便 我认为这是一个错误的API 关键这是一个正确的假设

+0

非常感谢。我遇到了同样的问题。 –

0

Althought的gapi.client.load有第三个参数是可选的,但人们在使用promise时遇到错误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); 
      }); 
     }); 
    } 
+0

我想这一个。它没有帮助。谢谢 –

+0

你还看到同样的错误?或其他不同的人 –

相关问题