Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript获取gmail联系人Google联系人API_Javascript_Google Contacts Api - Fatal编程技术网

使用javascript获取gmail联系人Google联系人API

使用javascript获取gmail联系人Google联系人API,javascript,google-contacts-api,Javascript,Google Contacts Api,它总是说您请求的页面无效。 如何使用GoogleContactsAPI通过javascript获取联系人 我有有效的作用域和客户端ID google.load('gdata', '2.x'); debugger google.setOnLoadCallback(function () { if (window.location.hash == "") { if (!checkLogin()) { logMeI

它总是说您请求的页面无效。 如何使用GoogleContactsAPI通过javascript获取联系人 我有有效的作用域和客户端ID

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”“(此处为空值),如何获取令牌的值以获取联系人,请参见“帮助”

尝试使用我的朋友。生活会变得更轻松更美好


要使用Google plus获取联系人列表,请使用以下选项:-

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

函数auth(){
变量配置={
“客户端id”:“OAUTH\u客户端id”,
'范围':'https://www.google.com/m8/feeds'
};
gapi.auth.authorize(配置,函数(){
fetch(gapi.auth.getToken());
});
}
函数获取(令牌){
$.ajax({
url:“https://www.google.com/m8/feeds/contacts/default/full?access_token=“+token.access_token+”&alt=json”,
数据类型:“jsonp”,
成功:功能(数据){
log(JSON.stringify(data));
}
});
}
在HTML正文中:-

<button onclick="auth();">GET CONTACTS FEED</button>
获取联系人提要
输出将作为包含电话号码的联系人字段


确保使用正确的重定向uri从google开发者控制台获取客户端id。

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

因为这很麻烦,我在这里写了一篇博客:

基本上我就是这样解决的:

<!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>

var clientId='您的客户ID';
var apiKey='您的API代码';
var作用域https://www.googleapis.com/auth/contacts.readonly';
$(文档).on(“单击“,”.googleContactsButton”,函数(){
gapi.client.setApiKey(apiKey);
设置超时(授权);
});
函数authorize(){
gapi.auth.authorize({client_id:clientId,scope:scopes,immediate:false},handleAuthorization);
}
功能手动授权(授权结果){
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”,
功能(响应){
//在这里处理响应
控制台日志(响应);
});
}
}
获取我的联系人

每次单击按钮时弹出窗口都会闪烁,这有点问题。将下面的代码段添加到Wouters解决方案将停止弹出窗口的闪烁

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

//开发人:加伦·米什拉。
var clientId='您的客户Id';
var apiKey='您的Api密钥';
var作用域https://www.googleapis.com/auth/contacts.readonly';
$(文档)。在(“单击”、“.getGmailContact”上,函数(){
gapi.client.setApiKey(apiKey);
设置超时(授权);
});
函数authorize(){
gapi.auth.authorize({client_id:clientId,scope:scopes,immediate:false},handleAuthorization);
}
功能手动授权(授权结果){
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”,
功能(响应){
//在这里处理响应
//控制台日志(响应);
var条目=response.feed.entry;
var触点=[];
对于(变量i=0;i    <!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">
        // Developed By: Garun Mishra.
            var clientId = 'Your Client Id';
            var apiKey = 'Your Api Key';
            var scopes = 'https://www.googleapis.com/auth/contacts.readonly';

            $(document).on("click", ".getGmailContact", 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);
                                var entries = response.feed.entry;
                                var contacts = [];
                                for (var i = 0; i < entries.length; i++) {
                                    var contactEntry = entries[i];
                                    var contact = [];

                                    //console.log(contactEntry);
                                    // Get Full Name.
                                    if (typeof (contactEntry.gd$name) != "undefined") {
                                        if (typeof (contactEntry.gd$name.gd$fullName) != "undefined") {
                                            if (typeof (contactEntry.gd$name.gd$fullName.$t) != "undefined") {
                                                contact['name'] = contactEntry.gd$name.gd$fullName.$t;
                                            }
                                        }
                                    }

                                    // Get Phone Number
                                    if (typeof (contactEntry['gd$phoneNumber']) != "undefined") {
                                        var phoneNumber = contactEntry['gd$phoneNumber'];
                                        for (var j = 0; j < phoneNumber.length; j++) {
                                            if (typeof (phoneNumber[j]['$t']) != "undefined") {
                                                var phone = phoneNumber[j]['$t'];
                                                contact['phone'] = phone;
                                            }
                                        }
                                    }

                                    // get Email Address
                                    if (typeof (contactEntry['gd$email']) != "undefined") {
                                        var emailAddresses = contactEntry['gd$email'];
                                        for (var j = 0; j < emailAddresses.length; j++) {
                                            if (typeof (emailAddresses[j]['address']) != "undefined") {
                                                var emailAddress = emailAddresses[j]['address'];
                                                contact['email'] = emailAddress;
                                            }
                                        }
                                    }
                                    contacts.push(contact);


                                }
                                // To Print All contacts
                                console.log(contacts);

                                // You can fetch other information as per your requirement uncomment the given line and read the data.
                                //console.log(entries);
                            });

                }
            }
        </script>
        <script src="https://apis.google.com/js/client.js"></script>
        <button class="getGmailContact">Get My Gmail Contacts</button>
    </body>
</html>