如何将 OAuth 与 PHP 和 cURL 结合使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1522869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use OAuth with PHP and cURL?
提问by Trevor Bramble
I'm trying to authenticate to YouTube via their Data API and simply need to know how the headers should be translated from their example (below) to PHP+CURL function calls. The confusing part being the Authorization portion, which breaks name/value pairing with its own set of name and value pairs.
我正在尝试通过他们的数据 API 向 YouTube 进行身份验证,并且只需要知道如何将标题从他们的示例(如下)转换为 PHP+CURL 函数调用。令人困惑的部分是授权部分,它用自己的一组名称和值对打破了名称/值配对。
This documentationis all well and good except I don't know how to format what they require in the headers.
这个文档很好,除了我不知道如何格式化他们在标题中需要的内容。
Their example:
他们的例子:
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
This doesn't need to be fancy, I just need to do the key exchange for one account so I can upload videos automatically. I just don't know how to format the Authorization items into a headers array for my
这不需要花哨,我只需要为一个帐户进行密钥交换,这样我就可以自动上传视频。我只是不知道如何将授权项格式化为我的标题数组
curl_setopt($ch, CURLOPT_HEADER, $headers);
Help?
帮助?
回答by shadow_of__soul
i didn't used the youtube api before but i have made my own REST api using OAuth for a web app.
我之前没有使用过 youtube api,但我已经使用 OAuth 为网络应用程序制作了自己的 REST api。
the header should be: application/x-www-form-urlencoded and as the example say, the parameters as oauth_consumer_key, oauth_signature_method,oauth_signature etc.. need to be sent using post, so you need to put it like this:
标头应该是: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"));
i hope it helps :D
我希望它有帮助:D
Regards.
问候。

