如何将OAuth用于PHP和cURL? [英] How do I use OAuth with PHP and cURL?

查看:240
本文介绍了如何将OAuth用于PHP和cURL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过其数据API向YouTube进行身份验证,只需了解标题应如何从其示例(以下)转换为PHP + CURL函数调用。令人困惑的部分是授权部分,它使用自己的名称和值对来打断名称/值对。



此文档都很好,除非我不知道如何格式化他们在标题中需要的内容。 / p>

例如:

  POST / accounts / OAuthGetRequestToken HTTP / 1.1 
Host:https://www.google.com
Content-Type:application / x-www-form-urlencoded
授权: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,$标题); 

帮助?

解决方案

我没有使用youtube api之前,但我已经做了我自己的REST API使用OAuth的网络应用程序。



标题应该是:应用程序/ 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



尊敬。


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.

This documentation is 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?

解决方案

i didn't used the youtube api before but i have made my own REST api using OAuth for a web app.

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:

        $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

Regards.

这篇关于如何将OAuth用于PHP和cURL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆