How to get API key for user using Google's OAuth API?

Clash Royale CLAN TAG#URR8PPPHow to get API key for user using Google's OAuth API?
Maybe I'm not understanding how the Youtube Data API is supposed to work. But if I have a PHP application that will generate the oauth request and receive the callback (which I have), the data it sends back is not a user's API key.
So I don't know if I am doing this wrong, or what, but their documentation is giving me a headache and I have searched and searched and I don't even know if my process is right here:
Web Server / OAuth Request (php)
require_once 'vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfig('oauth.json');
$client->setAccessType("offline"); // offline access
$client->setIncludeGrantedScopes(true); // incremental auth
$client->addScope(Google_Service_YouTube::YOUTUBE_READONLY);
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
Client Authorizes my Application
Google sends back (whatever this is)
'code' => string 'BLABLABLABLABLA' (length=89)
'scope' => string 'https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile' (length=224)
Where is the user's API key, how do I get it? Is this not correct, or am I just jumbling stuff up here?
2 Answers
2
You don't get an API key when using OAuth. You get an access token. API key is obtained from a users's Google Developer Console.
Check the PHP Quickstart for a quick Youtube API OAuth reference in PHP.
your oauth2callback.php should look something like this. It will take the code and exchange it for an access token.
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/Oauth2Authentication.php';
// Start a session to persist credentials.
session_start();
// Handle authorization flow from the server.
if (! isset($_GET['code'])) {
$client = buildClient();
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client = buildClient();
$client->authenticate($_GET['code']); // Exchange the authencation code for a refresh token and access token.
// Add access token and refresh token to seession.
$_SESSION['access_token'] = $client->getAccessToken();
$_SESSION['refresh_token'] = $client->getRefreshToken();
//Redirect back to main script
$redirect_uri = str_replace("oauth2callback.php",$_SESSION['mainScript'],$client->getRedirectUri());
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
Code ripped from my sample project on github
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
you are getting an authorization code which you need to exchange for an access token. you will not be using an API key with oauth2. the library handes all this for you
– DaImTo
27 mins ago