[英]Google contacts API problems from JavaScript client with OAuth2 authentication
經過數小時的努力,Docs似乎很糟糕。 基本上我試圖讓讀訪問OAuth2用戶身份驗證的用戶接觸,即使用便攜式通訊錄API或完全成熟的聯系人API 。 Google 最近開始允許OAuth2 。
通過首先使用戶通過范圍“ https://www.google.com/m8/feeds”進行身份驗證,我可以通過Contacts API訪問用戶聯系人。 然后,我可以使用jQuery檢索他們的前25個聯系人(顯示的代碼為CoffeeScript )
$.ajax
url: "https://www.google.com/m8/feeds/contacts/default/full"
dataType: 'jsonp'
data: { access_token: token, alt: 'json-in-script' }
success: (data, status) ->
console.log "The returned data", data
那行得通,我得到了JSON數據。 但是,幾乎令人難以置信的是,Google提供的唯一聯系訂單(據我所知)是“ lastmodified ”(嚴重是wtf?)。 我需要更多類似“頂級朋友”或“最受歡迎”的東西。
恰好是Google Portable Contacts API 可以執行的操作 ,(是!)。 當然,我似乎無法成功申請工作。
首先,通過單擊此鏈接,使用戶通過便攜式聯系人API進行身份驗證(注意范圍:“ https://www-opensocial.googleusercontent.com/api/people”)
<a href="https://accounts.google.com/o/oauth2/authclient_id=457681297736.apps.googleusercontent.com&response_type=token&redirect_uri=http://localhost:3000/team&scope=https://www-opensocial.googleusercontent.com/api/people">Import Google Contacts</a>
效果很好,我獲得了回傳的訪問令牌。
接下來,我嘗試將ajax請求發送到便攜式聯系人API
$.ajax
url: "https://www-opensocial.googleusercontent.com/api/people/@me/@all"
dataType: 'jsonp'
data: { access_token: token, alt: 'json-in-script' }
success: (data, status) ->
console.log "The returned data", data
但這會返回403錯誤
403 (The currently logged in user and/or the gadget requesting data, does not have access to people data.
有什么想法我做錯了嗎?
附錄
我在Google OAuth2論壇中找到了此錯誤報告 ,該報告建議我們在使用Portable Contacts API時需要設置授權標頭。 所以我這樣嘗試:
$.ajax
url: "https://www-opensocial.googleusercontent.com/api/people/@me/@all"
dataType: 'jsonp'
data: { access_token: token, alt: 'json-in-script' }
beforeSend: (xhr) ->
xhr.setRequestHeader "Authorization", "OAuth #{token}"
data: { access_token: token }
success: (data, status) ->
console.log "The returned data", data
但這會讓我遇到相同的403錯誤:
403 (The currently logged in user and/or the gadget requesting data, does not have access to people data
問題是您顯然無法在JSONP請求上設置請求標頭。 有關更多信息,請參見此問題的答案。
據我所知,替代方案是:
Google應該不難。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.