2014-03-02 72 views
2

它总是说您请求的页面无效。 我如何可以获取使用谷歌联系人API 用JavaScript接触本人有效范围和客户端ID使用javascript获取Gmail联系人Google Contacts API

google.load('gdata', '2.x'); 
    debugger 
    google.setOnLoadCallback(function() { 
     if (window.location.hash == "") { 
      if (!checkLogin()) { 
       logMeIn(); 
      } else { 
       var feedUrl = "https://www.google.com/m8/feeds/contacts/default/full"; 
       query = new google.gdata.contacts.ContactQuery(feedUrl); 
       query.setMaxResults(5000); 
       myService = new google.gdata.contacts.ContactsService('exampleCo-exampleApp-1.0'); 
       myService.getContactFeed(query, function (result) { 
        document.cookie = "g314-scope-0="; 
        window.opener.parseGmailContacts(result.feed.entry); 
        close(); 
       }, function (e) { 
        alert(e.cause ? e.cause.statusText : e.message); 
       }); 
      } 
     } 
    }); 
    function logMeIn() { 
     scope = "https://www.google.com/m8/feeds"; 
     var token = google.accounts.user.login(scope); 
    } 
    function logMeOut() { 
     google.accounts.user.logout(); 
    } 
    function checkLogin() { 
     scope = "https://www.google.com/m8/feeds/"; 
     var token = google.accounts.user.checkLogin(scope); 
     return token; 
    } 

我觉得有什么不妥

var token = google.accounts.user.checkLogin(scope); 
      return token; 

令牌retuns“”(空值这里),我怎么能得到令牌的价值获取联系人,PLZ帮助

+0

我觉得有什么不妥 VAR为myService =新google.gdata.contacts.ContactsService( 'exampleCo-ExampleApp中-1.0');这 – TheDean

回答

0

有关读取使用谷歌联系人列表,以及使用: -

<script src="https://apis.google.com/js/client.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
<script> 
    function auth() { 
    var config = { 
     'client_id': 'OAUTH_CLIENT_ID', 
     'scope': 'https://www.google.com/m8/feeds' 
    }; 
    gapi.auth.authorize(config, function() { 
     fetch(gapi.auth.getToken()); 
    }); 
    } 

    function fetch(token) { 
    $.ajax({ 
    url: "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + token.access_token + "&alt=json", 
    dataType: "jsonp", 
    success:function(data) { 
       console.log(JSON.stringify(data)); 
    } 
}); 
} 

在HTML正文: -

<button onclick="auth();">GET CONTACTS FEED</button> 

输出将具有与现场包含电话号码的联系人。

请务必使用正确的重定向URI从Google开发者控制台获取客户端ID。

10

我遇到了同样的问题,我通过首先检索访问令牌来解决问题,然后直接调用API。这是因为javascript api(gapi)不支持检索谷歌联系人。

因为它是相当的麻烦,我写了一个博客帖子大约在这里:https://labs.magnet.me/nerds/2015/05/11/importing-google-contacts-with-javascript.html

基本上这就是我如何解决它:

<!DOCTYPE html> 
<html> 
    <head> 
    <meta charset='utf-8' /> 
    <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script> 
    </head> 
    <body> 
    <script type="text/javascript"> 
      var clientId = 'your Client ID'; 
      var apiKey = 'Your API Code'; 
      var scopes = 'https://www.googleapis.com/auth/contacts.readonly'; 

      $(document).on("click",".googleContactsButton", function(){ 
      gapi.client.setApiKey(apiKey); 
      window.setTimeout(authorize); 
      }); 

      function authorize() { 
      gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization); 
      } 

      function handleAuthorization(authorizationResult) { 
      if (authorizationResult && !authorizationResult.error) { 
       $.get("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + authorizationResult.access_token + "&max-results=500&v=3.0", 
       function(response){ 
        //process the response here 
        console.log(response); 
       }); 
      } 
      } 
     </script> 
     <script src="https://apis.google.com/js/client.js"></script> 
     <button class="googleContactsButton">Get my contacts</button> 
    </body> 
</html> 
+0

这从一个本地的HTML文件工作,但当我在我们的项目在服务器中使用它,我'CORS头'Access-Control-Allow-Origin'不匹配'*''。有任何想法吗? – skytreader

0

我有轻微的问题与弹出闪烁每次按钮被点击。将下面的代码片段添加到Wouters解决方案将停止弹出窗口。

function authorize(){ 
    if($scope.authorizationResult){ 
     handleAuthorization($scope.authorizationResult); 
    }else{ 
gapi.auth.authorize({client_id: clientId, scope: scopes, immediate:false}, handleAuthorization); 
     } 
    } 
相关问题