如何在PHP和cURL中使用OAuth?

时间:2009-10-05 23:11:05

标签: php curl oauth

我正在尝试通过他们的数据API对YouTube进行身份验证,只需要知道如何将标头从他们的示例(下面)转换为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

这不需要花哨,我只需要为一个帐户进行密钥交换,这样我就可以自动上传视频。我只是不知道如何将授权项格式化为我的

的标题数组
curl_setopt($ch, CURLOPT_HEADER, $headers);

帮助?

1 个答案:

答案 0 :(得分:18)

之前我没有使用过youtube api,但是我已经使用OAuth为web应用程序制作了自己的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

问候。