YouTube API v3 continually asks for authorization

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

I open a page, the application asks for authorization. I click the login link, select my gmail account and get the listing.

The problem is that when I go back to the application even after a few seconds, I have to authorize the application again.

I thought as soon as the app gets the token swap permission for the refresh token.

Is there somewhere that shows some code how to get the refresh token as the documentation or any link to it on the internet is very bad.

I really need help to get this working as I've been trying for the past two weeks and getting nothing.

+3


source to share


2 answers


I have a long answer, but that should be a big help. I had the same problem with poor documentation and am also trying to get a list of videos from a playlist for my project. API v3 magically started working for me after the battle for the last couple of days, here's what I did.

First, the API key was actually obtained. I'm sure you've already been to the Google Developers Console, but just in case, here's what you need to do for this step:

  • Create a project in the Google Developers Console
  • Enable YouTube Data API API
  • In the Credentials section, if you had the same problem as me, you can create a new tenant ID but not an API key.

This is a serious problem because you need an API key to easily request data without authorization, such as retrieving a video list from a playlist. To get your API key, go back to the API section, click on YouTube Data API 3, and here's a loading bar that never loads anything. However, you can click "Quota" here and go to the old version of the Developer Console.

In this old version, you can go to "API Access" and add Simple API Access keys (API Keys work in this older version, but not in the newer version of Developer Console). I could be wrong about that, but I think that " Create a new server key ... "for server-side languages ​​such as PHP, and" Create a new browser key ... "for client-side languages ​​such as Javascript. There are also buttons for Android and iOS keys, but I assume you don't need those. Anyway, I am using cURL in PHP and the Server key worked for me, I configured it to allow any sources while my project is still in development.

Once you get the Key for Server Applications in Easy API Access , it may take some time before all requests using this key actually work. For me, my script didn't work for about 4-5 hours (I think Google is having real problems with their servers this week and that's why I had to "trick" it by giving me an API key. Probably why the "Credentials "does not load anything).

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

Now it will give you a 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 enter the URL after the word GET and replace {YOUR_API_KEY} with your API key.

Now you can use this url in your cURL request, for example, where $ request_url is the url 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);

      

The $ resp will now contain a JSON string that you can parse and retrieve data for these videos. I'll leave the JSON parsing to you, but this is how you get data similar to the old gdata way in v2. And no authentication :)

It might report an error that your API key is not authorized, in which case you will need to wait a few hours. Again, I think Google has been having server issues lately, so please be patient; it's not your script that doesn't work, it's google;)

Note that

$resp = file_get_contents($request_url);

      

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

+3


source


I had to deal with this as I couldn't find examples of how to do it easily.

It turned out that I needed to create a POST request to get the accesscode and refresh the token, and this was achieved using the Curlfrom command line

url --data "code=AUHROISATON_CODE&client_id=CLIENT_ID.apps.googleusercontent.com&client_secret=CLINET_SECRET&redirect_uri=http://www.example.com&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token

      



After several errors like "Missing grant_type", "Invalid code" now works.

I revoked access to the app, made the following GET request (taken from https://developers.google.com/accounts/docs/OAuth2WebServer#formingtheurl ) to get the code and was able to use the code in the Curl description above

https://accounts.google.com/o/oauth2/auth ? scope = https://www.googleapis.com/auth/youtube& state = security_token% 3D138r5719ru3e1% 26url% 3Dhttps: //oa2cb.example.com/myHome& redirect_uri = HTTPS% 3A% 2F% 2Foauth2-login-demo.appspot. com% 2Fcode &, response_type = code & client_id = 812741506391.apps.googleusercontent.com & approval_prompt = strength

0


source







All Articles