Keyslinger
Keyslinger

Reputation: 5274

When implementing Google API sample, can't get past OAuth 2.0

I'm trying to use the PlaylistItems: insert YouTube Data API sample to add a YouTube video to a playlist programmatically with PHP.

I hosted the example at https://[mydomain].com/custom/yt/insert.php and ran the provided composer command in that directory to install the api client per the instructions.

I set the OAuth redirect address to https://[mydomain].com/custom/yt/insert.php in the API Console and downloaded the secrets.json file and hosted it in the same directory.

enter image description here

Screen shot showing API settings

When I run the insert.php script in my browser, I am taken to a page with the following:

Open the following link in your browser: https://accounts.google.com/o/oauth2/auth?response_type=code&access_type=offline&client_id=194988007367-cnn3jdo0c4j6njsvorpcv1s5jdhej6iu.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fdjpanaflex.com%2Fcustom%2Fyt%2Finsert.php&state&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube.force-ssl&approval_prompt=auto Enter verification code:

No verification code is provided, but when I visit that web address, I am taken through the authentication process and appear to successfully complete it.

However, the API call to add a video to a playlist is not executed:

playlistItemsInsert($service,
array('snippet.playlistId' => '[PLAYLIST ID OMITTED]',
       'snippet.resourceId.kind' => 'youtube#video',
       'snippet.resourceId.videoId' => '[VIDEO ID OMITTED]',
       'snippet.position' => ''),
'snippet', 
array('onBehalfOfContentOwner' => ''));

Instead, I am returned to the page prompting me to complete the authorization process.

Why is my API call not being made? Is my OAuth 2.0 authentication not being stored? If so, how can I correct this?

Upvotes: 0

Views: 218

Answers (1)

Keyslinger
Keyslinger

Reputation: 5274

It turns out Google's sample is designed to be run from a CLI and my browser-based implementation was not providing a means of prompting the user for input that is called for by a line in the getClient() function that takes input from STDIN.

Since the auth code is provided as a parameter in the $authUrl variable that gets printed to the screen the first time the script is run, I was able to resolve my issue by replacing

$authCode = trim(fgets(STDIN));

with:

$authCode = $_GET['code'];

Upvotes: 1

Related Questions