1

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?

1
  • 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 Commented Jul 31, 2018 at 12:09

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.