Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTube API v3 keeps asking for authorisation every time

I am using the YouTube api v3 to retrieve a list of videos using the example at https://developers.google.com/youtube/v3/code_samples/php#retrieve_my_uploads

I open the page, the app asks for authorisation. I click the link to authorise, select my gmail account and I get the listing.

The problem is when i go back to the app even only a few seconds later, I have to authorise the app again.

I thought once the app was authorised you could exchange a token for a refresh token.

Is there anywhere that shows some code how to get a refresh token as the documentation or any reference to it online is very poor.

I really need some help getting this working as i've been trying for the last couple weeks and getting nowhere.

like image 486
AdRock Avatar asked Sep 11 '14 11:09

AdRock


People also ask

Why is YouTube API not working?

My YouTube API Key is Not Working Your API key may not be working because you're using it for the wrong project. Be sure you're using the key for the project that you created it for, especially if you created multiple projects at the same time. If it's still not working, consider creating a new API key entirely.

How do I authorize YouTube API?

Create your project and select API services Open the Credentials page in the API Console. This API supports two types of credentials. Create whichever credentials are appropriate for your project: OAuth 2.0: Whenever your application requests private user data, it must send an OAuth 2.0 token along with the request.

Is YouTube data API v3 free?

Yes, using the YouTube API does not incur any monetary cost for the entity calling the API. If you go over your quota an 403 Error will be returned by the API.

Does YouTube API have a limit?

Projects that enable the YouTube Data API have a default quota allocation of 10,000 units per day, an amount sufficient for the majority of our API users. You can see your quota usage on the Quotas page in the API Console.


1 Answers

I have a long-winded answer, but it should be a big help. I had the same issue with the poor documentation and I am also trying to get a list of videos from a playlist for my project. The API v3 just magically started working for me after struggling for the past couple days, here is what I did.

First was actually getting the API key. I'm sure you've been in the Google Developers Console by now, but just in case here is what to do for this step:

  1. Create a project in Google Developers Console
  2. Enable the YouTube Data API v3 API
  3. Under "Credentials", if you had the same issue as me, you can create a new Client ID but not an API key.

This is a major problem because you need an API key to make simple data requests without authorization, e.g., getting a list of videos from a playlist. To get an API key, go back to the "APIs" section, click on "YouTube Data API v3", and here is the screen with the loading indicator that never loads anything. However you can click on "Quota" here and it takes you to the older version of the Developers Console.

From this older version, you can go to "API Access" and add your Simple API Access keys (the API keys work on this older version, but not on the new version of Developers Console). I could be wrong about this but I think "Create new Server key..." is for server-side languages like PHP, and "Create new Browser key..." is for client-side languages like Javascript. There are also buttons for Android and iOS keys, but I assume you don't need those. Anyway, I'm using cURL in PHP and the Server key worked for me, I have it set to allow any referrers while my project is still in development.

Once you get your Key for server apps under Simple API Access, it might be a while before any requests using the key actually work. For me, my script didn't work for about 4-5 hours (I think Google is having real issues with their servers this week, which is also why I had to "trick" it into giving me an API key. And it's probably why the "Credentials" page doesn't load anything).

Now use the tool on https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.playlists.list to help create your GET request. Enter "snippet" in the part field and an integer for maxResults if necessary. Then get the ID of a playlist from the URL on Youtube. For example, Conan's "Clueless Gamer" series is https://www.youtube.com/playlist?list=PLVL8S3lUHf0RqD7TZ6hohWk8Sd3asaqnY, so the ID is PLVL8S3lUHf0RqD7TZ6hohWk8Sd3asaqnY. Then click Execute.

Now it will give you the GET Request, something like

GET https://www.googleapis.com/youtube/v3/playlists?part=snippet&id=PLVL8S3lUHf0RqD7TZ6hohWk8Sd3asaqnY&maxResults=20&key={YOUR_API_KEY}

X-JavaScript-User-Agent:  Google APIs Explorer

Just take the URL after the word GET and replace {YOUR_API_KEY} with your API key.

Now you can use this URL in a cURL request, like so, where $request_url is the URL from above with your API key in it:

//http://codular.com/curl-with-php
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $request_url,
    CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

Now $resp will hold a JSON string that you can parse through and get the data for that playlist's videos. I'll leave the JSON parsing up to you, but that's how you get data similar to the old gdata way in v2. And no authentication :)

It might report an error back that your API key isn't authorized, in which case you'll need to wait several hours. Again, I think Google has been having real server issues lately so be patient; it's not your script that doesn't work, it's Google ;)

I will note that

$resp = file_get_contents($request_url);

seems to work as well, but I honestly don't know which method is "better" between cURL and file_get_contents().

like image 85
James Avatar answered Nov 15 '22 08:11

James