Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use OAuth with PHP and cURL?

Tags:

php

curl

oauth

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?

like image 427
Trevor Bramble Avatar asked Oct 05 '09 23:10

Trevor Bramble


1 Answers

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.

like image 87
shadow_of__soul Avatar answered Nov 12 '22 01:11

shadow_of__soul