如何使用google-api-javascript-client或“Contacts API 3.0版”从gmail导入联系人?

时间:2012-08-13 10:43:07

标签: javascript google-contacts google-api-client

我使用2.0版本的Contacts API和Gdata库来导入客户gmail信息。此版本不再受支持,我尝试移至V3,但我看到vd不支持Gdata,我花了一天试图修改当前代码以使用javascript的“Contacts API 3.0版”。

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Gmail Login</title>
        <meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
        <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    </head>
    <body style="margin:0;padding:0;">
        <img src="/images/templates.png" style="display:none;"/>
        <script type="text/javascript">
            google.load("gdata", "2.s");
            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;
            }
        </script>
    </body>
    </html>

Google Contacts API 3.0版是否支持javascript客户端或gdata库?

1 个答案:

答案 0 :(得分:7)

var authParams = gapi.auth.getToken() // from Google oAuth

authParams.alt = 'json';

$.ajax({
  url: 'https://www.google.com/m8/feeds/contacts/default/full',
  dataType: 'jsonp',
  data: authParams,
  success: function(data) { console.log(data); }
});

基本上只需将其插入google api javascript库中提供的authSample.html即可 - https://code.google.com/p/google-api-javascript-client/