YouTube API v3 每次都要求授权

YouTube API v3 keeps asking for authorisation every time(YouTube API v3 每次都要求授权)
本文介绍了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 数据 API v3 API
  3. 在凭据"下,如果您遇到与我相同的问题,您可以创建新的客户端 ID,但不能创建 API 密钥.

这是一个主要问题,因为您需要一个 API 密钥才能在未经授权的情况下发出简单的数据请求,例如,从播放列表中获取视频列表.要获取 API 密钥,请返回API"部分,单击YouTube 数据 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 访问"并添加您的 Simple API 访问 密钥(API 密钥适用于旧版本,但不适用于新版本的开发者控制台).我可能错了,但我认为创建新的服务器密钥..."适用于 PHP 等服务器端语言,而创建新的浏览器密钥..."适用于 Javascript 等客户端语言.还有用于 Android 和 iOS 键的按钮,但我认为您不需要这些.无论如何,我在 PHP 中使用 cURL 并且服务器密钥为我工作,我将它设置为允许任何引用者,而我的项目仍在开发中.

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.

Simple API Access 下获得服务器应用密钥 后,可能需要一段时间才能使使用该密钥的任何请求真正生效.对我来说,我的脚本在 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 请求.如有必要,在 part 字段中输入snippet"并为 ma​​xResults 输入一个整数.然后从 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 是上面的 URL,其中包含您的 API 密钥:

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

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 每次都要求授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)