OAuth學習筆記

翻譯自下面這篇文章
https://developer.yahoo.com/oauth/guide/oauth-auth-flow.html

概述

OAuth是一套在開發(fā)者和服務提供者之間建立溝通機制的協(xié)議杭攻;( OAuth是讓第三方應用不需要用戶名密碼讀取用戶數據的一個認證過程。)在本文檔里垒棋,服務提供者是yahoo叼架,訪問用戶數據的應用或者站點被稱作consumer

整體流程圖示

主體內容如下:

步驟1. 注冊yahoo并獲取consumer Key

在通過api訪問yahoo之前,需要先注冊并提交若干信息(包括訪問數據的范圍)以獲取使用yahoo服務的資格扮饶。在以后通過consumer訪問數據的時候乍构,yahoo會詢問最終用戶授權給consumer訪問數據的范圍哥遮,關于數據范圍的說明如下:

  • Read/Write Yahoo Updates
  • Read (Shared) Yahoo Profiles
  • Read Yahoo
    注冊完成,會拿到consumer key和consumer secret奥帘,在獲取request token的時候仪召,必須作為參數傳入。
步驟2. 獲取request token

request token是用來完成用戶認證時需要傳入的臨時token已旧,通過consumer key來獲得运褪。
請求URL:
https://api.login.yahoo.com/oauth/v2/get_request_token?oauth_nonce=ce2130523f788f313f76314ed3965ea6&oauth_timestamp=1202956957&oauth_consumer_key=123456891011121314151617181920&oauth_signature_method=plaintext&oauth_signature=abcdef&oauth_version=1.0&xoauth_lang_pref="en-us"&oauth_callback="http://yoursite.com/callback"

參數說明:

Request Parameter Description
oauth_consumer_key Consumer Key provided to you when you signed up.
oauth_nonce A random string (OAuth Core 1.0 Spec, Section 8)
oauth_signature_method The signature method that you use to sign the request. This can be PLAINTEXT or HMAC-SHA1.
oauth_signature The Consumer Secret that was issued to the application. If you are using the PLAINTEXT signature method, add %26 at the end of the Consumer Secret. For more information about signing requests, refer to Signing Requests to Yahoo.
oauth_timestamp Current timestamp of the request. This value must be +-600 seconds of the current time.
oauth_version OAuth version (1.0).
xoauth_lang_pref (optional) The language preference of the User; the default value is EN-US. For further details about this parameter, refer to the OAuth Extension for Specifying User Language Preference.
oauth_callback Yahoo redirects Users to this URL after they authorize access to their private data. If your application does not have access to a browser, you must specify the callback as oob (out of bounds).

request token是用來完成用戶認證時需要傳入的臨時token,通過consumer key來獲得店读。

返回值

Request Parameter Description
oauth_token_secret The secret associated with the Request Token, provided in hexstring format.
oauth_expires_in The lifetime of the Request Token in seconds. The default number is 3600 seconds, or one hour.
xoauth_request_auth_url The URL to the Yahoo authorization page.
oauth_token The Request Token that Yahoo returns as a response to therequest_token call. The Request Token is required during the User authorization process.
oauth_callback_confirmed=true This parameter confirms that you are using OAuth 1.0 Rev. A. This parameter is always set to true.
步驟3. 用戶登錄授權

拿到request token之后屯断,應用提供給用戶yahoo的授權頁面侣诺,用戶通過該頁面客戶授予應用訪問他在yahoo上數據的權限

如下圖所示:

如果用戶沒有登錄的話,會先彈出登錄界面趴久;

請求參數

Request Parameter Description
oauth_token The Request Token that Yahoo returns as a response to the request_token
call. The Request Token is required during the User authorization process.

下面的參數會添附在 獲取request token的時候傳入的回調地址后面

Callback URL Parameter Description
oauth_token The Request Token that Yahoo returns as a response to the get_request_token call. It is appended to the authorization page URL. The Request Token is required during the User authorization process.
oauth_verifier The OAuth Verifier is a verification code tied to the Request Token. The OAuth Verifier and Request Token both must be provided in exchange for an Access Token. They also both expire together. If the oauth_callback is set to oob in Step 2, the OAuth Verifier is not included as a response parameter and is instead presented once the User grants authorization to your application. Yahoo instructs the User to enter the OAuth Verifier code in your application. Your application must ask for this OAuth Verifier code to ensure OAuth authorization can proceed. The OAuth Verifier is intentionally short so that a User can type it manually.

如下圖所示:


yahoo認證頁面有兩種方法展現

  • 彈出式窗口
  • 從web應用直接重定向到y(tǒng)ahoo認證頁
步驟4. 用request Token和oauth_verifier來換取access token

用戶授權之后consumer需要將request token變換為access token

請求URL:
https://api.login.yahoo.com/oauth/v2/get_token?oauth_consumer_key=dj0yJmk9NG5USlVvTlZsZEpnJmQ9WVdrOVQwa&oauth_signature_method=PLAINTEXT&oauth_version=1.0&oauth_verifier=svmhhd&oauth_token=gugucz&oauth_timestamp=1228169662&oauth_nonce=8B9SpF&oauth_signature=5f78507cf0acc38890cf5aa697210822e90c8b1c

請求參數

Request Parameter Description
oauth_consumer_key Consumer Key provided to you when you signed up.
oauth_signature_method The signature method that you use to sign the request. This can be PLAINTEXT or HMAC-SHA1.
oauth_nonce A random string ([OAuth Core 1.0 Spec, Section 8]http://oauth.net/core/1.0#nonce))
oauth_signature The concatenated Consumer Secret and Token Secret separated by an "&" character. If you are using the PLAINTEXT signature method, add %26 at the end of the Consumer Secret. If using HMAC-SHA1, refer to [OAuth Core 1.0 Spec, Section 9.2]http://oauth.net/core/1.0#anchor16). For more information about signing requests, refer toSigning Requests to Yahoo.
oauth_timestamp Current timestamp of the request. This value must be +-600 seconds of the current time.
oauth_verifier The OAuth Verifier is a verification code tied to the Request Token.
oauth_version OAuth version (1.0).
oauth_token The Request Token, which is required during the User authorization process and is short enough for the end User to easily enter. The Request Token is provided in the response to the get_request_token

請求URL:
oauth_token=A%3DqVDHXBngo1tEtzox.JMhzd91Rk99.39Al7hos3J80mm1j
&oauth_token_secret=c5a9684d3a3aa22aa051308987219efb8d6982fc
&oauth_expires_in=3600
&oauth_session_handle=AKVdNElJthnrHDwnYDuj6fJ2ayRbJvkePz9AKwi9dQAfb4bd
&oauth_authorization_expires_in=919314350
&xoauth_yahoo_guid=DKXSX6Q5TA5SVNARZLUJU5AW7A

返回值

Response Parameter Description
oauth_token The Access Token provides access to protected resources accessible through Yahoo Web services.
oauth_token_secret The secret associated with the Access Token provided in hexstring format.
oauth_session_handle The persistent credential used by Yahoo to identify the Consumer after a User has authorized access to private data. Include this credential in your request to refresh the Access Token once it expires.
oauth_expires_in Lifetime of the Access Token in seconds (3600, or 1 hour).
oauth_authorization_expires_in Lifetime of the oauth_session_handle in seconds.
xoauth_yahoo_guid The introspective GUID of the currently logged in User. For more information of the GUID, see the [Yahoo Social API Reference]https://developer.yahoo.com/social/rest_api_guide/introspective-guid-resource.html).

獲得access token之后就可以訪問yahoo的服務(如果是用彈出式窗口的方式提供用戶授權的話,那么獲取到access token之后確保關閉彈出窗口)

步驟5. 刷新access Token

access token一個小時后失效弛作,繼續(xù)訪問的話需要更新access token
請求URL:
https://api.login.yahoo.com/oauth/v2/get_token?oauth_nonce=ef3a091928d5491624c0ac54d697124422705091&oauth_consumer_key=123456891011121314151617181920&oauth_signature_method=plaintext&oauth_signature=55d4cf6bf417023ce5dcc3b77132fb021cd13b21abcdef%26&oauth_version=1.0&oauth_token=AJwxz0eyXRa._q0xKlggmMSpQzYjOZyqApyhHybqPzCsDr0-&oauth_timestamp=1204762971&oauth_session_handle=ALKVBsl8DHR1rsAHSwTmAxYIsIGs3l31syRaA_aaF.RDs.MknmVM4P

請求參數

Request Parameter Description
oauth_nonce A random string ([OAuth Core 1.0 Spec, Section 8]http://oauth.net/core/1.0#nonce))
oauth_consumer_key Consumer Key provided to you when you sign up on the egistration page.
oauth_signature_method The signature method that you use to sign the request. This can be PLAINTEXT or HMAC-SHA1.
oauth_signature The concatenated Consumer Secret and Token Secret separated by an "&" character. For more information about signing requests, refer to Signing Requests to Yahoo.
oauth_timestamp Current timestamp of the request. This value must be +-600 seconds of the current time.
oauth_version OAuth version (1.0).
oauth_token The expired Access Token.
oauth_session_handle The persistent credential used by Yahoo to identify the Consumer after a User has authorized access to private data. Include this credential in your request to refresh the Access Token once it expires.

返回值

Response Parameter Description
oauth_nonce A random string ([OAuth Core 1.0 Spec, Section 8]http://oauth.net/core/1.0#nonce))
oauth_consumer_key Consumer Key provided to you when you sign up on the registration page.
oauth_signature_method The signature method that you use to sign the request. This can be PLAINTEXT or HMAC-SHA1.
oauth_signature The concatenated Consumer Secret and Token Secret separated by an "&" character. For more information about signing requests, refer to Signing Requests to Yahoo.

同時來自阮一峰的一篇非常好的文章 理解OAuth 2.0

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市萨西,隨后出現的幾起案子诸尽,更是在濱河造成了極大的恐慌,老刑警劉巖穿肄,帶你破解...
    沈念sama閱讀 211,265評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件咸产,死亡現場離奇詭異仲闽,居然都是意外死亡,警方通過查閱死者的電腦和手機赖欣,發(fā)現死者居然都...
    沈念sama閱讀 90,078評論 2 385
  • 文/潘曉璐 我一進店門顶吮,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人搏恤,你說我怎么就攤上這事湃交。” “怎么了息罗?”我有些...
    開封第一講書人閱讀 156,852評論 0 347
  • 文/不壞的土叔 我叫張陵才沧,是天一觀的道長俏扩。 經常有香客問我录淡,道長油坝,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,408評論 1 283
  • 正文 為了忘掉前任彬檀,我火速辦了婚禮瞬女,結果婚禮上,老公的妹妹穿的比我還像新娘坤学。我一直安慰自己报慕,他們只是感情好,可當我...
    茶點故事閱讀 65,445評論 5 384
  • 文/花漫 我一把揭開白布飞苇。 她就那樣靜靜地躺著蜗顽,像睡著了一般。 火紅的嫁衣襯著肌膚如雪忿等。 梳的紋絲不亂的頭發(fā)上刊懈,一...
    開封第一講書人閱讀 49,772評論 1 290
  • 那天虚汛,我揣著相機與錄音皇帮,去河邊找鬼。 笑死将谊,一個胖子當著我的面吹牛冷溶,可吹牛的內容都是我干的尊浓。 我是一名探鬼主播,決...
    沈念sama閱讀 38,921評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼苗胀,長吁一口氣:“原來是場噩夢啊……” “哼基协!你這毒婦竟也來了菇用?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 37,688評論 0 266
  • 序言:老撾萬榮一對情侶失蹤杂穷,失蹤者是張志新(化名)和其女友劉穎揩慕,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體拴鸵,經...
    沈念sama閱讀 44,130評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡蜗搔,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,467評論 2 325
  • 正文 我和宋清朗相戀三年樟凄,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片汰现。...
    茶點故事閱讀 38,617評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡叔壤,死狀恐怖,靈堂內的尸體忽然破棺而出嗅战,到底是詐尸還是另有隱情俺亮,我是刑警寧澤疟呐,帶...
    沈念sama閱讀 34,276評論 4 329
  • 正文 年R本政府宣布启具,位于F島的核電站珊泳,受9級特大地震影響,放射性物質發(fā)生泄漏旨椒。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,882評論 3 312
  • 文/蒙蒙 一涣仿、第九天 我趴在偏房一處隱蔽的房頂上張望示惊。 院中可真熱鬧,春花似錦米罚、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,740評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽动看。三九已至,卻和暖如春菱皆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背京痢。 一陣腳步聲響...
    開封第一講書人閱讀 31,967評論 1 265
  • 我被黑心中介騙來泰國打工历造, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留船庇,地道東北人。 一個月前我還...
    沈念sama閱讀 46,315評論 2 360
  • 正文 我出身青樓臣淤,卻偏偏與公主長得像窃爷,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子按厘,可洞房花燭夜當晚...
    茶點故事閱讀 43,486評論 2 348

推薦閱讀更多精彩內容