YouTube API v3 keeps asking for authorisation every time

后端 未结 2 1328
攒了一身酷
攒了一身酷 2021-01-12 12:34

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

相关标签:
2条回答
  • 2021-01-12 13:05

    Took me ages to work this out as I couldn't find any examples on how to do this easily.

    It turned out that I need to create a POST request to get an access code and refresh token and this was achieved with using Curlfrom the 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 a few errors such as 'Missing grant_type', 'Invalid code' it now works.

    I revoked access to the app, ran 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 above Curl statement

    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=force

    0 讨论(0)
  • 2021-01-12 13:18

    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().

    0 讨论(0)
提交回复
热议问题