PHP OAuth Tutorial

150 views
Skip to first unread message

Paul Sawaya

unread,
Jun 18, 2009, 5:18:24 PM6/18/09
to justintv-ap...@googlegroups.com
I've posted a bare-bones tutorial of getting OAuth authenticated on the wiki. Comments and questions welcome!

http://apiwiki.justin.tv/mediawiki/index.php/OAuth_PHP_Tutorial

gm

unread,
Jul 29, 2009, 8:43:50 AM7/29/09
to Justin.tv API Developers
Hi Paul,
Thanks a lot for your tutorial; I am completely new to oAuth.php and i
can't find much documentation on how to use it with these API.
What I am trying to do is to to set a callback to be notified when the
broadcasting is finished.

this is my code

$end_callback_req=$access_token_req->from_consumer_and_token
($consumer,
$access_token, "POST", "http://api.justin.tv/api/
stream /register_callback.xml",array
("channel"=>"my_channel","event"=>"stream_down","url"=>"my_url"));

$end_callback_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(),
$consumer,$access_token);
$end_callback = doHttpRequest($end_callback_req->to_postdata());

i am then trying to get a list of the registered callbacks using...

$callbacks_req=$access_token_req->from_consumer_and_token($consumer,
$access_token, "GET", "http://api.justin.tv/api/stream/
list_callbacks.xml",array("channel"=>"my_channel"));
$callbacks_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(),
$consumer,$access_token);
$callbacks = doHttpRequest($callbacks_req->to_url());

Noting is returned from the above code and nothing is posted to the
callbak url.
Could you please tell me where i am wrong or point me to some php
examples?

Emmett Shear

unread,
Jul 30, 2009, 1:17:09 PM7/30/09
to justintv-ap...@googlegroups.com
Hi GM -

Unfortunately, Paul was a summer intern who's gone back to school. I'll see if I can help you though I don't know much PHP.

Looking at your code, I have a couple questions:
 - In the first line, you use a variable $access_token - what's the flow to generate that $access_token?
 - Can you add some print statements to see what the result of $end_callback is? And the result of $callbacks?

E

gm

unread,
Aug 1, 2009, 5:07:13 AM8/1/09
to Justin.tv API Developers
Hi Emmett,
First of all thanks a lot for trying to help me.
The $access_token variable come from the beginning of the script
callback.php of the tutorial and it seems to be correct because i am
using it to retrieve my steam_key which is returned properly.

//We were passed these through the callback.
$token = $_REQUEST['token'];
$token_secret = $_REQUEST['token_secret'];

$consumer = new OAuthConsumer($key, $secret, NULL);
$auth_token = new OAuthConsumer($token, $token_secret);
$access_token_req = new OAuthRequest("GET",
$oauth_access_token_endpoint);
$access_token_req = $access_token_req->from_consumer_and_token
($test_consumer,
$auth_token, "GET", $oauth_access_token_endpoint);

$access_token_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1
(),$consumer,
$auth_token);

$after_access_request = doHttpRequest($access_token_req->to_url
());
parse_str($after_access_request,$access_tokens);

$access_token = new OAuthConsumer($access_tokens['oauth_token'],
$access_tokens['oauth_token_secret']);

$streamkey_req = $access_token_req->from_consumer_and_token
($consumer,
$access_token, "GET", "http://api.justin.tv/api/
channel/stream_key.xml");

$streamkey_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(),
$consumer,$access_token);

$after_request = doHttpRequest($streamkey_req->to_url());

//Get streamkey from returned XML
$stream_key = parseStream_KeyFromXML ($after_request);

Until here everything is working fine and i can properly broadcast
using the embedded broadcaster
Regarding the returned values of $end_callback and $callbacks as i
said they are empty; no errors an no values;

Thank you very much your help is very appreciated



On Jul 31, 12:17 am, Emmett Shear <emmett.sh...@gmail.com> wrote:
> Hi GM -
>
> Unfortunately, Paul was a summer intern who's gone back to school. I'll see
> if I can help you though I don't know much PHP.
>
> Looking at your code, I have a couple questions:
>  - In the first line, you use a variable $access_token - what's the flow to
> generate that $access_token?
>  - Can you add some print statements to see what the result of $end_callback
> is? And the result of $callbacks?
>
> E
>
> On Wed, Jul 29, 2009 at 5:43 AM, gm <gianmichele.bartol...@gmail.com> wrote:
>
> > Hi Paul,
> > Thanks a lot for your tutorial; I am completely new to oAuth.php and i
> > can't find much documentation on how to use it with these API.
> > What I am trying to do is to to set a callback to be notified when the
> > broadcasting is finished.
>
> > this is my code
>
> >  $end_callback_req=$access_token_req->from_consumer_and_token
> > ($consumer,
> >                $access_token, "POST", "http://api.justin.tv/api/
> > stream    /register_callback.xml",array
> > ("channel"=>"my_channel","event"=>"stream_down","url"=>"my_url"));
>
> >  $end_callback_req->sign_request(new OAuthSignatureMethod_HMAC_SHA1(),
> > $consumer,$access_token);
> >  $end_callback = doHttpRequest($end_callback_req->to_postdata());
>
> > i am then trying to get a list of the registered callbacks using...
>
> >  $callbacks_req=$access_token_req->from_consumer_and_token($consumer,
> >                $access_token, "GET", "http://api.justin.tv/api/stream/
> > list_callbacks.xml <http://api.justin.tv/api/stream/%0Alist_callbacks.xml>

Luis Campos

unread,
Jun 26, 2013, 12:24:56 PM6/26/13
to justintv-ap...@googlegroups.com
Hi, I need your help I go through the tutorial you posted, but I'm getting this error "Failed to get API stream_key!"

I have my Key and sercret, and I downloaded OAuth.php

I have to do something special?

thanks

regards
Reply all
Reply to author
Forward
0 new messages