[分享] Google API Oauth 2.0 教程範例

Oauth 2.0 開始需要事先註冊project。
http://code.google.com/apis/console#access

[![](http://1.bp.blogspot.com/-iAxFCGZ8m_A/TmiBqTqxUnI/AAAAAAAANyE/UHN11r5BSlo/s640/20110908_1.jpg)](http://1.bp.blogspot.com/-iAxFCGZ8m_A/TmiBqTqxUnI/AAAAAAAANyE/UHN11r5BSlo/s1600/20110908_1.jpg)
專案開啟完之後,接著需要參考文件 [http://code.google.com/intl/zh-TW/apis/accounts/docs/OAuth2.html](http://code.google.com/intl/zh-TW/apis/accounts/docs/OAuth2.html) []() 接著需要些基本的Sample code [http://code.google.com/intl/zh-TW/apis/accounts/docs/OAuth2.html#ClientLibraries](http://code.google.com/intl/zh-TW/apis/accounts/docs/OAuth2.html#ClientLibraries)
[![](http://1.bp.blogspot.com/-CjmTL37wWP4/TmiCT1HWuAI/AAAAAAAANyI/sCIoikgyW9M/s640/20110908_2.jpg)](http://1.bp.blogspot.com/-CjmTL37wWP4/TmiCT1HWuAI/AAAAAAAANyI/sCIoikgyW9M/s1600/20110908_2.jpg)
以PHP client 為例,需要修改** google-api-php-client/src/config.php** ` // The application_name is included in the User-Agent HTTP header.

'application_name' => '',



// OAuth2 Settings, you can get these keys at https://code.google.com/apis/console

'oauth2_client_id' => '',

'oauth2_client_secret' => '',

'oauth2_redirect_uri' => '',



// The developer key, you get this at https://code.google.com/apis/console

'developer_key' => '',

` 這些資訊都必須填入,注意!oauth2_redirect_uri必須和project 填寫資訊相同。 **相關** 在找相關資訊的時候也找到這個不錯的文章介紹 [http://sudocode.net/article/430/get-a-users-google-email-address-via-oauth2-in-php/](http://sudocode.net/article/430/get-a-users-google-email-address-via-oauth2-in-php/) [LIVE DEMO](http://clonn.info/demo/gdata/oauth2.php)
[![](http://2.bp.blogspot.com/-VEOsyQBWJ4c/TmiInPq7rCI/AAAAAAAANyM/BlY5YASUIqs/s400/20110908_3.jpg)](http://2.bp.blogspot.com/-VEOsyQBWJ4c/TmiInPq7rCI/AAAAAAAANyM/BlY5YASUIqs/s1600/20110908_3.jpg)
要求使用者授權。
[![](http://1.bp.blogspot.com/-TFUwAUKpWlE/TmiJSCsdLlI/AAAAAAAANyU/i_zGD9c3e60/s400/20110908_5.jpg)](http://1.bp.blogspot.com/-TFUwAUKpWlE/TmiJSCsdLlI/AAAAAAAANyU/i_zGD9c3e60/s1600/20110908_5.jpg)
取得所有聯絡人資訊
[![](http://4.bp.blogspot.com/-Aeg-yy2ysFI/TmiMTbR2_wI/AAAAAAAANyY/ZSTdDa4hMUw/s400/20110908_6.jpg)](http://4.bp.blogspot.com/-Aeg-yy2ysFI/TmiMTbR2_wI/AAAAAAAANyY/ZSTdDa4hMUw/s1600/20110908_6.jpg)
**後記**
發現網路上的資料大部分都以Oauth 1.0居多,2.0的資料十分少量,可能是因為Google api 目前Oauth 1.0, 2.0 並行授權的關係吧!筆記一下這些資料,希望下次進行撰寫API會有些幫助。

CaesarChi

Web developer, focus on website fullstack, special JavaScript, and love sharing developing experience and communicate with developers. http://about.me/clonn