❶ Using OAuth 2.0 for Login( Google のドキュメント )
上のドキュメントではログインする為の URL のサンプルがありますが、一般アプリケーションでブロウザコントロールを使う上において、以下が最低限必要なものです( 見やすいように改行を付加しています )
https://accounts.google.com/o/oauth2/auth?
client_id=424911365001.apps.googleusercontent.com&
response_type=code&
scope=openid%20email&
redirect_uri=https://oa2cb.example.com/
ベースは『https://accounts.google.com/o/oauth2/auth』ですが、必要なものの詳細は以下のようになります。
➀ client_id : Google Developer Console より取得
➁ response_type : code で固定
➂ scope : 使用する API によって違います
➃ redirect_uri : client_id と供に登録されている URL
scope は、例えば Google Drive であれば https://www.googleapis.com/auth/drive.file+https://www.googleapis.com/auth/drive+https://www.googleapis.com/auth/drive.appdata となります。
また、カレンダーなら https://www.googleapis.com/auth/calendar+https://www.googleapis.com/auth/calendar.readonly+https://www.googleapis.com/auth/plus.me です。
❷ URL 内の code の取得
『リダイレクトURL?code=必要なデータ』という単純な内容なので、『必要なデータ』を取り出して、それを使って正式なアクセストークンを POST で取得します。( Exchange Code for Access Token and ID Token )
❸ https://accounts.google.com/o/oauth2/token へPOST
※ 見やすいように改行を付加しています
※ grant_type=authorization_code は固定です
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=必要なデータ&
client_id=Client ID&
client_secret=Client secret&
redirect_uri=Redirect URIs&
grant_type=authorization_code
❹ JSON で返されるアクセストークン
token_type は、Bearer で固定のようですが、このアクセストークンを使う時に必要なので、保存して使用します。( Authorization ヘッダで、アクセストークンの前にセットする / 区切りはスペース )
{
"access_token" : "アクセストークン",
"token_type" : "Bearer",
"expires_in" : 3599
}
関連する記事(実装)
❶ VS2010(C#) Form : Google API でログインして Google ドライブにアップロード
❷ Windows8(C#) で Google にログインしてカレンダーの情報を扱う
posted by
at 2013-07-19 13:00
|
API
|

|