Google oauth 1.0

2024-04-19 22:38
文章标签 1.0 google oauth

本文主要是介绍Google oauth 1.0,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

下面的linkoauth 1.0的最好中文讲解(它是用twitteroauth

http://cire.pixnet.net/blog/post/30810748-%E6%BC%AB%E8%AB%87oauth%E8%AA%8D%E8%AD%89%E5%8D%94%E5%AE%9A%E8%88%87%E9%81%8B%E4%BD%9C%E6%B5%81%E7%A8%8B

 

仔细阅读上面的文章后,下面讲解googleoauth1.0

参考文档:

OAuth 1.0 API Reference

http://code.google.com/intl/zh-TW/apis/accounts/docs/OAuth_ref.html

OAuth 1.0 for Web Applications

http://code.google.com/intl/zh-TW/apis/accounts/docs/OAuth.html

OAuth 1.0 for Installed Applications

http://code.google.com/intl/zh-TW/apis/accounts/docs/OAuthForInstalledApps.html

UsingOAuth with theGoogle Data APIs

http://code.google.com/intl/zh-TW/apis/gdata/articles/oauth.html

 

Google oauth 1.0PHP demo (playground demo)

http://googlecodesamples.com/oauth_playground/

source

http://code.google.com/p/gdata-samples/source/browse/#svn/trunk/oauth_playground

example description

http://code.google.com/intl/zh-TW/apis/gdata/articles/oauth.html

 

Google oauth 1.0java demo

http://oauthexample.appspot.com/Welcome

source

http://code.google.com/p/googleappengine/source/browse/#svn%2Fbranches%2F1.2.1%2Fjava%2Fdemos%2Foauth

 

什么是oauth?

OAuthOpen Authorization的縮寫, 透過這種協定, 使用者可以在不透露帳號密碼的情況下, 授權第三方網路應用服務(在協定中稱為Consumer)使用(或登入)原本的網路服務(在協定稱為Service Provider) 例如我們可以利用oauth的方式在自己撰寫的網站登入twitter,google, yahoofoursquare等網站, 並使用其開放出來的網路服務。

 

使用Googleoauth 1.0 for webapplication之前,必须先在https://accounts.google.com/ManageDomainsregister a domain来生成一个OAuth Consumer Key和一个OAuth Consumer Secretregistration的参考文档见http://code.google.com/intl/zh-TW/apis/accounts/docs/RegistrationForWebAppsAuto.html

 


 

其中的"Target URL path prefix"oauth好像没有,只是对authsub有用。它只是个前缀而已,并不是完整的回转的路径,在生成登录的时候,还是要将回转的路径发送给Google,而且必须和这里注册的前缀相符,这应该算是用来保障安全的一个设置。其值应该是在使用authsub时用,authSub authorizationrequest”next” parameter必须以该值作为前缀!例如,若”next” parameter值为http://example.com/authsub orhttp://example.com/feed/authsub,那么Target URL path prefix” option的值应为http://example.com

 

通过Googleoauth1.0access usergoogle data的调用流程(分4步):

 

Step1 通过OAuthGetRequestToken获取request token

Send a request with following parameter tohttps://www.google.com/accounts/OAuthGetRequestToken来获取requesttoken

 

Parameter listhttp://code.google.com/intl/zh-TW/apis/accounts/docs/OAuth_ref.html#RequestToken

 

Send request时,有三种方式来放置这些parameter

l      Authorization header of a GETor POST request. Use "Authorization: OAuth".除了” scope” and“xoauth_displaynameparam之外,其他parameters都可以放置在request header” scope” and “xoauth_displaynameparam必须放在request body里或者放在URL as a query parameter.

Sample request (使用Authorization header of a POST request)

POST/accounts/OAuthGetRequestToken HTTP/1.1

Host:www.google.com

Content-Type:application/x-www-form-urlencoded

Authorization:OAuth

oauth_consumer_key="example.com",

oauth_signature_method="RSA-SHA1",

oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",

oauth_timestamp="137131200",

oauth_nonce="4572616e48616d6d65724c61686176",

oauth_version="1.0"

oauth_callback="http://www.example.com/showcalendar.html"

 

scope="http://www.google.com/calendar/feedshttp://picasaweb.google.com/data"

 

l      Body of a POST request. Thecontent type must be "Content-Type: application/x-www-form-urlencoded".

 

l      As URL query parameters in aGET request.

 

 

如果该requestresponse code is 200,则返回的response会包含下列info

l      oauth request token (unauthorized)

l      token secret

l      confirmation that Google handles a callback URL

 

Sample response

oauth_token=ab3cd9j4ks73hf7g&oauth_token_secret=ZXhhbXBsZS5jb20&oauth_callback_confirmed=true

 

Step2 通过OAuthAuthorizeTokenAuthorize request token of step 1

step 1获取的unauthorizedoauth_token作为parameter “oauth_tokensend GET request tohttps://www.google.com/accounts/OAuthAuthorizeToken

(requestfull parameter list参看http://code.google.com/intl/zh-TW/apis/accounts/docs/OAuth_ref.html#GetAuth)

 

Sample request

GET https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=ab3cd9j4ks73hf7g&hd=mycollege.edu&hl=en&btmpl=mobile

 

google获取该request,就会redirect to Google Access Request page (user还没有login google,会先转到google login pagelogin成功后才转去Google Access Request page),如下图


user click “Grantaccess” button后,之前request里的参数oauth_token引用的oauth requesttoken (unauthorized)就会被authorizedresponse返回的authorized request token "oauth_token"和step 1获取的unauthorizedoauth request token的值相同

 

如果在step 1里有设置oauth_callback参数来提供callback URL,那么当user click “Grant access” button后,google就会redirect到该callback URL with following query parameters:

l      oauth_token”, authorized request tokentoken的值与step 1获取的unauthorizedoauth request token的值相同)

l      oauth_verifier”,verification code.

 

Sample redirect url with query parameter

http://www.example.com/showcalendar.html?oauth_token=ab3cd9j4ks73hf7g&oauth_verifier=fERNOO3NfWph90CPCeIutmFA

 

如果没有提供call backURL,就不会redirect,而是show message with a verification code

 

 

Step 3通过OAuthGetAccessToken来把step 2Authorize request token转化为accesstoken (token才能access user google data)

Send a request with following parameter tohttps://www.google.com/accounts/OAuthGetAccessToken来获取accesstoken

 

Parameter listhttp://code.google.com/intl/zh-TW/apis/accounts/docs/OAuth_ref.html#AccessToken

 

Send request时,有三种方式来放置这些parameter

l      Authorization header of a GETor POST request. Use "Authorization: OAuth".

Sample request (使用Authorization header of a POST request)

POST/accounts/OAuthGetAccessToken HTTP/1.1

Host: www.google.com

Content-Type:application/x-www-form-urlencoded

Authorization:OAuth

oauth_consumer_key="example.com",

oauth_token="ab3cd9j4ks73hf7g",

oauth_verifier="fERNOO3NfWph90CPCeIutmFA",

oauth_signature_method="RSA-SHA1",

oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",

oauth_timestamp="137131200",

oauth_nonce="4572616e48616d6d65724c61686176",

oauth_version="1.0"

 

l      Body of a POST request. Thecontent type must be "Content-Type: application/x-www-form-urlencoded".

 

l      As URL query parameters in aGET request.

 

 

如果该requestresponse code is 200,则返回的response会包含下列info

l      Access token

l      token secret

 

Sample response

oauth_token=ab3cd9j4ks73hf7g&oauth_token_secret=ZXhhbXBsZS5jb20

 

注意:unauthorizedrequest token, authorized request token and access token都是以parameter“oauth_token” valueresponse里返回

 

Step 4利用step 3获取的access token就可以通过各种googleapiaccessusergoogle data

 

 

SigningOAuth requests

从上面的step 1OAuthGetRequestToken and step 3OAuthGetAccessToken,在send request时都必须包含oauth_signature”parameter。实际上all requests made to Google services都需要包含该parameter.

 

那么如何生成该parameter的值呢?很简单,就是通过oauth_signature_method” parameter设置的signature method来对一个“base string”。

 

Unregisteredapplications (没有register domainapp,即没有comsumer key and secret) should use HMAC-SHA1signature method. Registeredapplications can use either RSA-SHA1 or HMAC-SHA1signaturemethod

“base string”由下列几部分组成

l      TheHTTPrequest method. 例如GET or POST

l      ThebaseURLthe request is being sent to.URL不应该包含any query parameters.

l      A normalizedstring of the parameters in the request (excluding theoauth_signature parameter).这包括所有写在requestheader or body里的parameter,以及添加在request URL里的query parameters. To normalize the string, sort the parameters using lexicographicalbyte value ordering.

 

base string” example

假设你已经获取了accesstoken,你正在通过GoogleCalendar API来获取a user'slist of calendarsrequest URL应该是:

http://www.google.com/calendar/feeds/default/allcalendars/full?orderby=starttime

request还有一些parameters,那么“Signature base string”应该类似于

GET&http%3A%2F%2Fwww.google.com%2Fcalendar%2Ffeeds%2Fdefault%2Fallcalendars%2Ffull&oauth_consumer_key%3Dexample.com%26oauth_nonce%3D4572616e48616d6d65724c61686176%26oauth_signature_method%3DRSA-SHA1%26oauth_timestamp%3D137131200%26oauth_token%3D1%252Fab3cd9j4ks73hf7g%26oauth_version%3D1.0%26orderby%3Dstarttime

 

注意: the orderby=starttime query parameter is ordered along with the rest ofthe oauth_* parametersin the base string

 

有了oauth signature ”basestring” and “method”,通过下列方式之一来生成oauth_signature” parameter value

l      If your application is notregistered, select HMAC-SHA1 and use the following key and secret:

                                consumer key: "anonymous"

                                consumer secret: "anonymous"

l      If your application isregistered and you're using RSA-SHA1, use the private key corresponding to thecertificate uploaded to Google during registration.

l      If your application isregistered and you're using HMAC-SHA1, use the OAuth"consumer secret" value generated during registration; this value isdisplayed on your domain's registration page.

 

Revoking(废除)anOAuth access token

Oauth access token可以通过手动方式或者programm方式来废除

l      Manuallyrevoking a token (for Google account holders)

1.         Users canmake changes to their Google Accounts settings athttps://www.google.com/accounts/. For Google Apps (hosted) domain accounts, gotohttps://www.google.com/a/yourdomain.com/ManageAccount.

2.         Click thelink "Change authorized websites".

3.         In thelist of authorized domains displayed, locate the domain to be revoked. Clickthe "Revoke Access" link next to the domain name.

 

l      Programmaticallyrevoking a token (for third-party applications)

Toprogrammatically revoke a token,send a request toAuthSub's AuthSubRevokeTokenendpoint with the necessary signedOAuthrequest.

 

 

 

 

这篇关于Google oauth 1.0的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/918632

相关文章

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法   消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法 [转载]原地址:http://blog.csdn.net/x605940745/article/details/17911115 消除SDK更新时的“

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException异常

用Gson解析json数据的时候,遇到一个异常,如下图: 这个异常很简单,就是你的封装json数据的javabean没有写对,你仔细查看一下javabean就可以了 比如:我的解析的代码是             Gson gson = new Gson();             ForgetJson rb = gson.fromJson(agResult.mstrJson, For

Google Earth Engine——高程数据入门和山体阴影和坡度的使用

目录 山体阴影和坡度 对图像应用计算 应用空间减速器 高程数据 通过从“重置”按钮下拉菜单中选择“清除脚本”来清除脚本。搜索“elevation”并单击 SRTM Digital Elevation Data 30m 结果以显示数据集描述。单击导入,将变量移动到脚本顶部的导入部分。将默认变量名称“image”重命名为“srtm”。使用脚本将图像对象添加到地图: Map

The import com.google cannot be resolved

The import com.google cannot be resolved,报错: 第一感觉就是缺少jar包,因为项目用maven管理,所以在pom.xml中添加: <dependency>  <groupId>com.google.code.gson</groupId>  <artifactId>gson</artifactId>  <version>2.3.1</ver

Node.js和vue3实现GitHub OAuth第三方登录

Node.js和vue3实现GitHub OAuth第三方登录 前言 第三方登入太常见了,微信,微博,QQ…总有一个你用过。 在开发中,我们希望用户可以通过GitHub账号登录我们的网站,这样用户就不需要注册账号,直接通过GitHub账号登录即可。 效果演示 注册配置 GitHub 应用 1.首先登录你的GitHub然后点击右上角的头像->点击进入Settings页面 2.在

C++常见异常汇总(三): fatal error: google/protobuf/port_def.inc

文章目录 1、fatal error : sw/redis++/redis.h2、fatal error: dwarf.h: No such file or directory3、fatal error: elfutils/libdw.h: No such file or directory4、fatal error: libunwind.h: No such file or directo

概率DP (由一道绿题引起的若干问题。目前为一些老题,蒟蒻的尝试学习1.0)

概率DP: 利用动态规划去解决 概率 期望 的题目。 概率DP 求概率(采用顺推) 从 初始状态推向结果,同一般的DP类似,只是经历了概率论知识的包装。 老题: 添加链接描述 题意: 袋子里有w只白鼠,b只黑鼠,A和B轮流从袋子里抓,谁先抓到白色谁就赢。A每次随机抓一只,B每次随机 抓完一只后 会有另外一只随机老鼠跑出来。如果两个人都没有抓到白色,那么B赢。A先抓,问A赢得概率。 w b 均在

解决OAuth Token,点击退出登录报404问题

首先,认证服务器发送请求 http://auth.test.com:8085/logout?redirect_uri=http://admin.test.com:8080’ 退出后报404无法跳转到网站首页,这个时候增加一个参数redirect_uri指定退出成功后跳转的路径,因为是自定义的,所以需在认证服务器做一些处理 找到源码默认实现接口DefaultLogoutPageGeneratingF

AI聊天应用不能上架?Google play对AI类型应用的规则要求是什么?

随着生成式AI模型的广泛应用,很多开发者都有在开发AI应用或将其整合到应用中。我们知道,谷歌是非常注重应用生态的,去年开始就推出了一些针对生成式AI应用的政策,对AI应用的内容质量和合规性问题提出了一些要求。 几天前,还有开发者聊到,现在AI类型应用(如AI聊天)上架越来越难了。 (可斯信进qun与众多开发者交流上架经验) 这很可能是没了解清楚Google play 对AI应用的一些

Google play最新政策更新和重要提醒

我们都知道,谷歌会定期更新其政策,而政策的变更通常对开发者及其团队的要求会更为严格,也会增加应用上架的一些限制条件,以此提高应用在谷歌商店的质量。 一起来看看Google play最近的一些政策更新和需要注意的地方。 新政策要求 对于提供金融产品和服务、健康服务、VPN、政府相关服务的开发者,需要注册为“企业”开发者账号才能提审上架应用。 Google play这个举措主要