我正在尝试获取谷歌联系人的照片。来自api guide发现了这个:
https://www.google.com/m8/feeds/photos/media/{userEmail}/{contactId}
我的代码:
`$.get("https://www.google.com/m8/feeds/photos/media/default/54b8abe0f52ad02?access_token=" + authorizationResult.access_token + "&v=3.0",
function(response){
//process the response here
console.log(response);
}
);`
它给了我这个错误:
否'访问控制 - 允许 - 来源'标头出现在请求的资源上。起源' http://localhost:3000'因此不允许访问。
但这似乎工作正常
`$.get("https://www.google.com/m8/feeds/contacts/default/full?alt=json&access_token=" + authorizationResult.access_token + "&max-results=500&v=3.0",
function(response){
//process the response here
console.log(response);
}
);`
修改 完整的js脚本:
`
<script type="text/javascript">
var clientId = 'my client id';
var apiKey = 'api key';
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/photos/media/default/54b8abe0f52ad02?access_token=" + authorizationResult.access_token + "&v=3.0",
function(response){
//process the response here
console.log(response);
}
);
}
}
</script>
`
答案 0 :(得分:1)
通过这样做我得到了图片:(带有代理网址的前缀图片网址)
` function handleAuthorization(authorizationResult) {
if (authorizationResult && !authorizationResult.error) {
console.log(authorizationResult);
var accessToken = authorizationResult.access_token;
$.get("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + accessToken + "&max-results=500&v=3.0",
function(response){
//process the response here
console.log(response);
let photoUrl = response.feed.entry[2].link[0].href + "&access_token=" + accessToken;
let proxy = 'https://cors.now.sh/';
let finalPhotoUrl = proxy + photoUrl;
document.getElementById('photo').src = finalPhotoUrl;
}
);
}
}`