带有 Google JavaScript 客户端库的 Google Contacts API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14104309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Contacts API with Google JavaScript Client Lib
提问by einsA
I am trying to work with the Google Contacts API v3.
我正在尝试使用Google Contacts API v3。
Because of the OAuth2 authentication and authorization I'm started with the Google APIs Client Library for JavaScript. I have noproblems with that part of the API access.
由于 OAuth2 身份验证和授权,我开始使用Google APIs Client Library for JavaScript。我对 API 访问的那部分没有问题。
But after doing the auth part I don't know what to do next. Can I use the google-api-javascript-clientfor the Google Contacts API v3? In the list of the supported Google APIs by the javascript-clientthe contacts API does not appear. But I have full access with the OAuth 2.0 Playground tool.
但是在完成身份验证部分之后,我不知道下一步该做什么。我可以将google-api-javascript-client用于 Google Contacts API v3 吗?在javascript 客户端支持的 Google API 列表中,没有出现contacts API。但我可以完全访问OAuth 2.0 Playground 工具。
I am really confused which client is working best with the latest Google Contacts API. What is about the gdata-javascript-client?
我真的很困惑哪个客户端最适合最新的 Google Contacts API。是什么样的GDATA的JavaScript客户端?
回答by ganoro
To use the v3 contacts api with the authentication token provided by gapi (Google JS client lib) this one is helpful, using alt=&json
要将 v3 联系人 api 与由 gapi(Google JS 客户端库)提供的身份验证令牌一起使用,这很有帮助,使用 alt=&json
$.getJSON('https://www.google.com/m8/feeds/contacts/default/full/?access_token=' +
authResult.access_token + "&alt=json&callback=?", function(result){
console.log(JSON.stringify(result));
});
回答by Danny Boyd
I know it's an old question but this question shows up when looking on how to read the contacts information from a Google account.
我知道这是一个老问题,但是在查看如何从 Google 帐户读取联系人信息时会出现这个问题。
If you only need to access the contact to import it or show the email, phone numbers, or other information and you don't need to modify it, you can use the People API (https://developers.google.com/people/). For javascript you can check the samples tab.
如果您只需要访问联系人以将其导入或显示电子邮件、电话号码或其他信息而无需对其进行修改,则可以使用 People API ( https://developers.google.com/people /)。对于 javascript,您可以查看示例选项卡。
I created a gist, which is almost the sample from Google but adding the requestField parameters.
我创建了一个 gist,它几乎是来自 Google 的示例,但添加了 requestField 参数。
回答by Brendan
The problem you are encountering is that the Contacts API v3 is an older API that works with the deprecated GData Client Library. Therefore it is incompatible with the newer Google APIs JavaScript Client.
您遇到的问题是 Contacts API v3 是一个较旧的 API,可与已弃用的GData Client Library配合使用。因此它与较新的Google APIs JavaScript Client不兼容。
For now you will need to load and use the GData Client library. For further information on the difference between the GData library and the Google APIs client, please refer to this recent SO question: gapi.client.load versus google.load
现在您需要加载和使用 GData 客户端库。有关 GData 库和 Google API 客户端之间差异的更多信息,请参阅最近的 SO 问题:gapi.client.load vs google.load
回答by MLU
Unfortunate Google Contacts API does not work with the new Javascript Client Library. It only works with GData Client Library. I tried working GData Client Library, but it is difficult as you get warnings in the documentation at every juncture that the library has been deprecated.
不幸的是,Google Contacts API 不能与新的 Javascript 客户端库一起使用。它仅适用于 GData 客户端库。我尝试使用 GData 客户端库,但是这很困难,因为您每次都会在文档中收到该库已被弃用的警告。
Therefore, I used a hydrid,
因此,我使用了氢化物,
- using the new Client Library, to get an authentication.
- Use a URL to get the contacts
- 使用新的客户端库,以获得身份验证。
- 使用 URL 获取联系人
Unfortunately, because of cross domain restrictions you need to use JSONP, otherwise the browser fails.
不幸的是,由于跨域限制,您需要使用 JSONP,否则浏览器会失败。
<script src="https://apis.google.com/js/client.js"></script>
.....
function contactsInit() {
var clientId = 'YOURCLIENTID.apps.googleusercontent.com';
var scopes = 'https://www.google.com/m8/feeds';
gapi.auth.authorize({
client_id: clientId, scope: scopes, immediate: false},
handleAuthResult);
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
var url =
"https://www.google.com/m8/feeds/contacts/default/" +
"full?alt=json-in-script&access_token=" +
authResult.access_token +
"&max-results=7000&v=3.0";
var myJSONP = new Request.JSONP({
url: url,
callbackKey: 'jsoncallback',
data: {
},
onRequest: function(url){
// a script tag is created with a src equal to url
},
onComplete: function(data){
// the request was completed.
}
}).send();
}
}
}
function Skeleton() {}
if (!gdata) {
var gdata = new Skeleton();
gdata.io = new Skeleton();
gdata.io.handleScriptLoaded = function(data) {
processContacts(data);
}
}
Notes: I use Mootools for JSONP but you could also use jquery or vanilla js with How to make a JSONP request from Javascript without JQuery?
注意:我将 Mootools 用于 JSONP,但您也可以使用 jquery 或 vanilla js 以及 如何在没有 JQuery 的情况下从 Javascript 发出 JSONP 请求?
You need to provide your own YOURCLIENTID, and define the processContacts function.
您需要提供自己的 YOURCLIENTID,并定义 processContacts 函数。
The gdata.io.handleScriptLoaded(data) is necessary since this what the url expects during callback.
gdata.io.handleScriptLoaded(data) 是必要的,因为这是 url 在回调期间所期望的。
I use a limit of 7000, but I don't think it is necessary.
我使用了 7000 的限制,但我认为没有必要。
If you don't want to use JSONP you could forward the access_token to your webserver, and process the URL there, e.g. with cURL or with Node.js just replace json-in-script with json.
如果您不想使用 JSONP,您可以将 access_token 转发到您的网络服务器,并在那里处理 URL,例如使用 cURL 或 Node.js 只需将 json-in-script 替换为 json。
In json-in-script is important on a browser since otherwise the browser croaks.
在 json-in-script 中,浏览器很重要,否则浏览器会发出嘶嘶声。
Thanks to the other answers on this page, that pointed me in the right direction.
感谢本页上的其他答案,这为我指明了正确的方向。
I hope that Google will make the Contacts API capable with the new Javascript Client Library. I hope that others will other be able to use this solution in the meantime.
我希望 Google 将通过新的 Javascript 客户端库使 Contacts API 具有能力。我希望其他人在此期间能够使用此解决方案。
回答by firebolt_ash
For fetching list of contacts using Google plus use this :-
要使用 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));
}
});
}
}
In the HTML Body :-
在 HTML 正文中:-
<button onclick="auth();">GET CONTACTS FEED</button>
The output will have as a field with the contact containing the phone number.
输出将作为包含电话号码的联系人的字段。
Make sure to get the client id from google developer console with proper redirect uri.
确保使用正确的重定向 uri 从谷歌开发者控制台获取客户端 ID。
回答by JoseAngel ConchelloBueyes
This is what we found to work to get individual data:
这是我们发现可以用来获取个人数据的方法:
var data = (JSON.stringify(data.feed.entry[0].gd$email, null, 4));
console.log(data);
If you run JSON.stringify(data) you can see all of the headers that you can call on.
如果您运行 JSON.stringify(data) ,您可以看到您可以调用的所有标头。
回答by mtaleb
google contact v3 is your best friend for this job
google contact v3 是你这份工作最好的朋友
here you can find all posible request endpoint https://developers.google.com/google-apps/contacts/v3/
在这里您可以找到所有可能的请求端点 https://developers.google.com/google-apps/contacts/v3/
like for all contact list this is the end point https://developers.google.com/google-apps/contacts/v3/#retrieving_all_contacts
对于所有联系人列表,这是终点 https://developers.google.com/google-apps/contacts/v3/#retrieving_all_contacts
After get authentication you can do get request to this url to get all contact list this is an example in python http://code.google.com/p/gdata-python-client/source/browse/samples/contacts/contacts_example.py
获得身份验证后,您可以请求此 url 以获取所有联系人列表,这是 python http://code.google.com/p/gdata-python-client/source/browse/samples/contacts/contacts_example 中的一个示例。 py