YouTube API v3每次都在要求授权 [英] YouTube API v3 keeps asking for authorisation every time

查看:145
本文介绍了YouTube API v3每次都在要求授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用YouTube api v3,以 https://developers.google.com/youtube/v3/code_samples/php#retrieve_my_uploads

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

我打开页面,应用程序请求授权.我单击链接进行授权,选择我的gmail帐户,然后得到列表.

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.

推荐答案

我的回答很漫长,但这应该有很大帮助.对于糟糕的文档,我也遇到了同样的问题,并且我也尝试从我的项目的播放列表中获取视频列表.经过几天的苦苦挣扎之后,API v3才开始为我神奇地工作,这就是我所做的.

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.

首先实际上是获取API密钥.我确定您目前已经进入Google Developers Console,但以防万一,这是该步骤的操作:

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. 在Google Developers Console中创建项目
  2. 启用YouTube Data API v3 API
  3. 在凭据"下,如果您遇到与我相同的问题,您可以创建一个新的客户端ID,但不能创建API密钥.

这是一个主要问题,因为您需要API密钥才能未经授权进行简单的数据请求,例如,从播放列表中获取视频列表.要获取API密钥,请返回"API"部分,单击"YouTube Data API v3",这是带有加载指示器的屏幕,该指示器永远不会加载任何内容.但是,您可以在此处单击配额",它会带您到开发人员控制台的旧版本.

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.

在此旧版本中,您可以转到"API访问权限"并添加简单API访问权限键(API键在此旧版本上有效,但在新版的Developers Console上无效) .我对此可能是错的,但我认为创建新的服务器密钥..."适用于服务器端语言(如PHP),而创建新的浏览器密钥..."适用于客户端语言(如Javascript).还有用于Android和iOS键的按钮,但我认为您不需要这些按钮.无论如何,我在PHP中使用cURL,并且Server键对我有用,我将其设置为在我的项目仍在开发中时允许任何引荐来源.

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.

简单API访问下获得服务器应用程序密钥后,可能需要一段时间才能真正使用该密钥进行任何请求.对我来说,我的脚本没有运行大约4-5个小时(我认为Google本周服务器出现了实际问题,这也是为什么我不得不欺骗"它给我一个API密钥的原因.这很可能是因为为什么凭据"页面不加载任何内容.

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

现在在 https://上使用该工具developers.google.com/apis-explorer/#p/youtube/v3/youtube.playlists.list 帮助创建GET请求.在部分字段中输入摘要",并在必要时为 maxResults 输入一个整数.然后从Youtube上的URL获取播放列表的ID.例如,柯南的无知玩家"系列是 https://www.youtube.com/playlist?list = PLVL8S3lUHf0RqD7TZ6hohWk8Sd3asaqnY ,因此ID为 PLVL8S3lUHf0RqD7TZ6hohWk8Sd3asaqnY .然后点击执行.

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.

现在它将向您发送GET请求,例如

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

只需在单词GET后加上URL,然后将{YOUR_API_KEY}替换为您的API密钥即可.

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

现在您可以在cURL请求中使用此URL,如下所示,其中 $ request_url 是上面带有API密钥的URL:

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);

现在 $ resp 将包含一个JSON字符串,您可以解析该字符串并获取该播放列表的视频数据.我将留给您JSON解析,但这是您如何获取与v2中旧的 gdata 方式相似的数据的方式.而且没有身份验证:)

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 :)

它可能会报告错误,表明您的API密钥未获得授权,在这种情况下,您需要等待几个小时.同样,我认为Google最近一直遇到服务器问题,因此请耐心等待;不是您的脚本不起作用,而是Google;)

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 ;)

我会注意

$resp = file_get_contents($request_url);

似乎也可以正常工作,但是老实说,我不知道在cURL和file_get_contents()之间哪种方法更好".

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

这篇关于YouTube API v3每次都在要求授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆