MySpace Developer Platform

A Place For Developers

Welcome Developers!

in

Welcome!

in

[PHP] oAuth verification failing when opensocial_token has a space.

Last post 06-10-2008 3:45 PM by EnigmatiK. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 06-10-2008 1:29 PM

    [PHP] oAuth verification failing when opensocial_token has a space.

    For some odd reason, this only works when opensocial_token (and, I suppose, any part of the query string) does not have a space in it. When it does, authentication fails. What's wrong? How exactly should the code handle spaces? (rawurlencode() doesn't solve the problem either.)

    (N.B. I cannot use Jerome's PHP Client Library as I have PHP 4 and the library is for PHP 5.)


    $oauth_sec = xxxxxxxxxxxxxxxx'; // Secret
    $remote_signature = $_GET['oauth_signature'];
    $url = strtolower('http://' . $_SERVER['SERVER_NAME'] . reset(explode('?', $_SERVER['REQUEST_URI'])));
    unset($_GET['oauth_signature']);
    ksort($_GET);
    $base_string = 'GET&' . urlencode($url) . '&' . urlencode(http_build_query($_GET));
    $secret = $oauth_sec . '&';

    $local_signature = base64_encode(hmac_sha1($secret, $base_string));

    if ($remote_signature == $local_signature) {
     echo 'Authenticated!';
    } else {
     echo 'Authentication failed!<br/>';
     echo 'Remote Sig: ' . $remote_signature . '<br/>';
     echo 'Local Sig: ' . $local_signature . '<br/>';
    }


    function hmac_sha1($key, $data) {
     // hash_hmac() is only for PHP5; this is for backwards compatibility.
     // From http://us.php.net/sha1 -- thanks Mark!
     $blocksize = 64;
     $hashfunc = 'sha1';
     if (strlen($key) > $blocksize) $key = pack('H*', $hashfunc($key));
     $key = str_pad($key, $blocksize, chr(0x00));
     $ipad = str_repeat(chr(0x36), $blocksize);
     $opad = str_repeat(chr(0x5c), $blocksize);
     $hmac = pack('H*', $hashfunc(($key ^ $opad) . pack('H*', $hashfunc(($key ^ $ipad) . $data))));
     return $hmac;
    }

  • 06-10-2008 2:42 PM In reply to

    Re: [PHP] oAuth verification failing when opensocial_token has a space.

    See the end of this thread:

    http://developer.myspace.com/Community/forums/t/2553.aspx?PageIndex=3

    Effectively you can't use http_build_query, you need to build the querystring yourself being sure to use rawurlencode()

  • 06-10-2008 3:45 PM In reply to

    Re: [PHP] oAuth verification failing when opensocial_token has a space.

    Ah, thanks. It's working smoothly now.

    If anyone's interested in my solution,

    $params = array();
    foreach ($_GET as $key => $val) $params[ = rawurlencode($key) . '=' . rawurlencode($val);
    $base_string = 'GET&' . rawurlencode($url) . '&' . rawurlencode(implode('&', $params));

Page 1 of 1 (3 items)