前提
PHPでGoogleのOAuth2.0を利用し、ユーザーにより認証されたGoogleアカウントに紐づくemailアドレスとYouTubeのチャンネルIDを取得しようとしています。
こちらのサンプルを参考にgoogle/apiclient libraryを利用しています。
実現したいこと
- ユーザーにより認証されたGoogleアカウントに紐づくemailとYouTubeのチャンネルIDを取得
発生している問題・エラーメッセージ
サンプルコードを参考にYouTubeのチャンネルIDを取得することはできましたが、
emailアドレスを取得することができません。
YouTube data api v3ではemailアドレスが取得できないようなので、
YouTube data apiとは別にuserinfoを利用する必要があると考えています。
2つのAPIのそれぞれのスコープを指定してユーザーに同時に同意を取り、access tokenを取得するイメージなのですが同時に2つのAPIを利用する方法がわからずご教示いただきたいです。
該当のソースコード
php
1if (!file_exists(__DIR__ . '/vendor/autoload.php')) { 2 throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"'); 3} 4require_once __DIR__ . '/vendor/autoload.php'; 5$OAUTH2_CLIENT_ID = 'xxxxxxxxxxxxxxxx'; 6$OAUTH2_CLIENT_SECRET = 'xxxxxxxxxxxxxx'; 7 8$client = new Google_Client(); 9$client->setClientId($OAUTH2_CLIENT_ID); 10$client->setClientSecret($OAUTH2_CLIENT_SECRET); 11$client->setScopes(['https://www.googleapis.com/auth/youtube.readonly','https://www.googleapis.com/auth/userinfo.email']); 12$redirect = filter_var('xxxxxxxxxxxxx', FILTER_SANITIZE_URL); 13$client->setRedirectUri($redirect); 14 15// Define an object that will be used to make all API requests. 16$youtube = new Google_Service_YouTube($client); 17 18// Check if an auth token exists for the required scopes 19$tokenSessionKey = 'token-' . $client->prepareScopes(); 20if (isset($_GET['code'])) { 21 if (strval($_SESSION['state']) !== strval($_GET['state'])) { 22 die('The session state did not match.'); 23 } 24 25 $client->authenticate($_GET['code']); 26 $_SESSION[$tokenSessionKey] = $client->getAccessToken(); 27 header('Location: ' . $redirect); 28} 29if (isset($_SESSION[$tokenSessionKey])) { 30 $client->setAccessToken($_SESSION[$tokenSessionKey]); 31} 32 33// Check to ensure that the access token was successfully acquired. 34if ($client->getAccessToken()) { 35 try { 36 $channelsResponse = $youtube->channels->listChannels('id', array( 37 'mine' => 'true', 38 )); 39 $htmlBody = ''; 40 foreach ($channelsResponse['items'] as $channel) { 41 $channel_id = $channel['id']; 42 } 43 } catch (Google_Service_Exception $e) { 44 $htmlBody = sprintf('<p>A service error occurred: <code>%s</code></p>', 45 htmlspecialchars($e->getMessage())); 46 print $htmlBody; 47 } catch (Google_Exception $e) { 48 $htmlBody = sprintf('<p>An client error occurred: <code>%s</code></p>', 49 htmlspecialchars($e->getMessage())); 50 print $htmlBody; 51 } 52}
試したこと
上記のコードにおいてはエラーは発生せずチャンネルIDを取得することができるのですが、ユーザーの認証においてuserinfo.emailに関する同意は表示されません。
setScopesの配列をuserinfo.emailのみにすると
Request had insufficient authentication scopes.
のエラーが出るので、emailの取得に関するリクエストができていないものと考えています。
スコープの指定方法自体はこちらを参考にしており、おそらく間違っていないと思うのですが、複数のAPIをまたぐリクエスト方法に問題があるのではないかと考えています。
補足情報(FW/ツールのバージョンなど)
こちらのサンプルを参考にgoogle/apiclient libraryを利用
あなたの回答
tips
プレビュー