因此,我正在开发一个将您的youtube频道数据与其他类似的youtube频道进行比较的应用程序。
当前,我正在使用其youtube帐户对用户进行身份验证,因此正在使用OAuth客户端进行身份验证。
我已经有了一个可以交换access_token
的代码,它也可以工作。
可悲的是,我得到的access_token
无法用于简单的/ search调用,而是给了我HTTP 400 -> keyinvalid
相同的调用可以很好地使用生成的ApiKey,但是我更喜欢使用OAutch。
我的问题是,accessToken是否不适用于/ search之类的公用电话?如果可以,我的问题在哪里?
预先感谢!
编辑:添加了代码示例
第1步:我在带有window.open(url)
的新窗口中使用以下url调用OAuth客户端
https://accounts.google.com/o/oauth2/v2/auth?
scope=https://www.googleapis.com/auth/youtube.readonly&
access_type=offline&
include_granted_scopes=true&
state=state_parameter_passthrough_value&
redirect_uri=https://website.com/&
response_type=code&
client_id=9791...7bng.apps.googleusercontent.com"
在响应中,我提取看起来像这样的代码
4/1QCLZnwsntp...sNcE
通过REST API将此代码发送到我的后端,后端使用此代码将其与access_token
交换为PHP curl POST call
$this->connection = curl_init();
$url = 'https://www.googleapis.com/oauth2/v4/token';
$param = 'code='.$this->credentialsTemp['code'].'&client_id='.$GLOBALS['Youtube-Credentials']['clientId'].'&client_secret='.$GLOBALS['Youtube-Credentials']['clientSecret'].'&redirect_uri=https://website.com/&grant_type=authorization_code';
curl_setopt($this->connection, CURLOPT_URL, $url);
curl_setopt($this->connection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->connection, CURLOPT_TIMEOUT, 20);
curl_setopt($this->connection, CURLOPT_POST,true);
curl_setopt($this->connection, CURLOPT_POSTFIELDS, $param);
$resp = curl_exec($this->connection);
curl_close($this->connection);
$acces_token = json_decode($resp,true)["access_token"];
$acces_token
中的应该是我的access_token,此刻看上去与该ya29.GlyTBtFMdLqKbvgaps...SA2pBORo_Am5UuBuxo8g
接近