OAuth2.0によるGoogle+ APIを試す



最近、Twitterのツイート情報をローカルに保存するために、OAuth1.0を調べていた。
CONSUMER_KEY、CONSUMER_SECRET、リクエストークン、ACCESS_TOKENなど実際に自分で
試してみるまでよく意味が分からなかった。
たまたま購読しているメールで、「OAuth2.0によるGoogle+ APIのアクセス方法」という情報が
あったので、OAuth2.0ではどのようにアクセスすれば良いのか試してみた。
Google Social Developers Japan
 

参考にした情報

OAuth2.0によるGoogle+ APIのアクセス方法
Google API Expert (Social)の方の記事をそのまま参考にさせて頂きました。
WEB+DB PRESS Vol.63
OAuthの特集記事があったので、基本的な概念と1.0 と 2.0のそれぞれの違いを確認しました。
 

試したこと(事前準備)

 事前準備:Google+にアカウントがあること
 

試したこと(事前準備)

1. Client IDの発行

OAuthで利用するための情報を発行します。
https://code.google.com/apis/console/ から登録を行いましょう。

Client ID for web applications

Client id:hogehage.apps.googleusercontent.com
Client secret:************************
Redirect URIs:http://localhost/oauth2callback
JavaScript origins:http://localhost

 
2. ユーザの認可

ServiceProviderにConsumerを登録しましょう。(OAuth2.0でも同じ役割かわかりません)

https://accounts.google.com/o/oauth2/auth?client_id=[YOUR client_id]&redirect_uri=[CALLBACK URL]&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.me&response_type=code

 
redirect_uriとscopeの値はURIエンコードをする。
上記のURLをブラウザで参照すると下記のような感じで表示がされます。

http://[CALLBACK URL]/oauth2callback?code=********

 
codeがAuthorization code値というものになるようです。
 
3. アクセストークンの取得

curlコマンドを利用して、アクセストークンを取得します。

curl -d client_id=[YOUR Client Id] -d client_secret=[YOUR Client Secret] -d redirect_uri=http%3A%2F%2Flocalhost%2Foauth2callback -d grant_type=authorization_code -d code=[YOUR Authorization code] https://accounts.google.com/o/oauth2/token

 
コマンドラインからアクセスを行うと下記の様な情報が返信されます。

{"access_token":"***","token_type":"Bearer","expires_in":3600,"refresh_token":"***"}

 
4. APIを利用して、Google+にアクセスしてみる

認可ユーザ自身のユーザ情報

curl -H "Authorization: OAuth [YOUR Access_token]" https://www.googleapis.com/plus/v1/people/me

 

{
"kind": "plus#person",
"id": "***",
"displayName": "***",
"gender": "male",
"aboutMe": "",
"url": "https://plus.google.com/***",
"image": {
"url": "https://lh5.googleusercontent.com/***/photo.jpg"
},
"urls": [
{
"value": "https://plus.google.com/***",
"type": "profile"
},
{
"value": "https://www.googleapis.com/plus/v1/people/***",
"type": "json"
}
],
"organizations": [
{
"name": "",
"title": "",
"type": "work"
}
],
"placesLived": [
{
"value": ""
},
{
"value": "",
"primary": true
}
]
}

 
認可ユーザの一般公開のフィード一覧

curl -H "Authorization: OAuth [YOUR Access_token]" https://www.googleapis.com/plus/v1/people/me/activities/public

 

{
"kind": "plus#activityFeed",
"nextPageToken": "***",
"selfLink": "https://www.googleapis.com/plus/v1/people/**/activities/public?",
"nextLink": "https://www.googleapis.com/plus/v1/people/**/activities/public?maxResults=20&pageToken=**",
"title": "Plus Public Activity Feed for **",
"updated": "2011-09-13T04:56:33.092Z",
"id": "tag:google.com,2010:/plus/people/**/activities/public",
"items": [
{
"kind": "plus#activity",
"title": "最近、Scalaでいろいろ楽しんでる。",
"published": "2011-09-13T04:56:32.000Z",
"updated": "2011-09-13T04:56:33.092Z",
"id": "**",
"url": "https://plus.google.com/**",
"actor": {
"id": "**",
"displayName": "**",
"url": "https://plus.google.com/**",
"image": {
"url": "https://lh5.googleusercontent.com/**/photo.jpg"
}
},
"verb": "post",
"object": {
"objectType": "note",
"content": "最近、Scalaでいろいろ楽しんでる。",
"originalContent": "",
"url": "https://plus.google.com/**",
"replies": {
"totalItems": 0
},
"plusoners": {
"totalItems": 0
},
"resharers": {
"totalItems": 0
}
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"items": [
{
"type": "public"
},
{
"type": "person",
"id": "**"
}
]
}
}
]
}

 
5. はまったこと

2のAuthorization code値を取得した後に、2時間程、放置してしまい「3. アクセストークンの取得」
を実施したところ下記のエラーがでた。

 {"error":"invalid_grant"}

 
Authorization code値には、有効期限があったようで、再度、Authorization code値を取得し、「3. アクセストークンの取得」を
実施したら正常にアクセストークンを取得できた。
 
「4. APIを利用して、Google+にアクセスしてみる」際に、https://www.googleapis.com/plus/v1/people/meアクセス時に
適切なヘッダー情報を付与しなかったら下記のようなエラーがでた。
curlコマンド

curl -H *** https://www.googleapis.com/plus/v1/people/me

 

{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit Exceeded. Please sign up",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit Exceeded. Please sign up"
}
}

 
エラーのメッセージからは、1日の利用制限に達したと読めたのだがyoichiro氏のOAuth2.0によるGoogle+ APIのアクセス方法
みると自分の発行したcurlコマンドからは、Authorization: OAuthというヘッダー情報が漏れていた。
 

今後

Google+ PlatformのAPIサイトにいろいろ情報が掲載されているので、いろいろ調べてみようと
思います。

https://developers.google.com/+/api/