OAuth学习笔记

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

概述

OAuth是一套在开发者和服务提供者之间建立沟通机制的协议;( 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应用直接重定向到yahoo认证页
步骤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一个小时后失效,继续访问的话需要更新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阅读 158,847评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,208评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,587评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,942评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,332评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,587评论 1 218
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,853评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,568评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,273评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,542评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,033评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,373评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,031评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,073评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,830评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,628评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,537评论 2 269

推荐阅读更多精彩内容