我正在尝试通过 YouTube 的数据 API 进行身份验证,并且只需要知道如何将 header 从其示例(如下)转换为 PHP+CURL 函数调用。令人困惑的部分是授权部分,它用自己的一组名称和值对破坏了名称/值配对。
This documentation一切都很好,只是我不知道如何格式化他们在标题中需要的内容。
他们的例子:
POST /accounts/OAuthGetRequestToken HTTP/1.1
Host: https://www.google.com
Content-Type: application/x-www-form-urlencoded
Authorization: OAuth
oauth_consumer_key="example.com",
oauth_signature_method="RSA-SHA1",
oauth_signature="wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D",
oauth_timestamp="137131200",
oauth_nonce="4572616e48616d6d65724c61686176",
oauth_version="1.0"
scope=http://gdata.youtube.com
这不需要很花哨,我只需要为一个帐户进行 key 交换,这样我就可以自动上传视频。我只是不知道如何将授权项格式化为我的标题数组
curl_setopt($ch, CURLOPT_HEADER, $headers);
帮忙?
最佳答案
我之前没有使用过 youtube api,但我已经使用 OAuth 为网络应用程序制作了自己的 REST api。
标题应该是:application/x-www-form-urlencoded 正如示例所示,oauth_consumer_key、oauth_signature_method、oauth_signature 等参数需要使用 post 发送,因此您需要这样写:
$header[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode("oauth_consumer_key=example.com&
oauth_signature_method=RSA-SHA1&
oauth_signature=wOJIO9A2W5mFwDgiDvZbTSMK%2FPY%3D&
oauth_timestamp=137131200&
oauth_nonce=4572616e48616d6d65724c61686176&
oauth_version=1.0"));
希望对您有帮助:D
问候。
关于php - 如何将 OAuth 与 PHP 和 cURL 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1522869/