google web サービスでアクセストークンの取得

google web サービスでアクセストークンの取得

#10 アクセストークンを取得しよう (1)
http://dotinstall.com/lessons/google_connect_php_v2/9910
を参考に
認証したユーザのアクセストークン取得処理を実装

アクセストークンの取得は
Handling the Response
をやっていく

リファレンスは
https://developers.google.com/accounts/docs/OAuth2WebServer?hl=ja#handlingtheresponse
を参考

code
client_id
client_secret
redirect_uri
grant_type
などのオプションをつかって

accounts.google.comのサイトの
/o/oauth2/token
へPOSTで投げかける

POSTで行うには
curl を使う

まず、パラメータを array() で作成する

1
2
3
4
5
6
7
$params = array(
    'client_id'=>CLIENT_ID,
    'client_secret'=>CLIENT_SECRET,
    'code'=>$_GET['code'],
    'redirect_url'=>SITE_URL.'redirect.php',
    'grant_type'=>'authorization_code'
);

とする

大文字の部分は、config.php で設定した定数

次に、アクセスするURLを変数へ格納

次に、curl を使っていくので
curl_init()

curl セッションを初期化し
新しい curl リソースを作成する

1
$url = curl_init();

curl_init() については
http://php.net/manual/ja/function.curl-init.php
を参照

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です