Google OAuth2 curl

Hi,

I’m working in Google OAuth2 and I am stuck in this part:

Source:
Using OAuth 2.0 to Access Google APIs - Authentication and Authorization for Google APIs - Google Code

«You can try out that call using the curl command line application, substituting the code you got from the above example URL—note that you may need to urlencode your redirect and secret, as curl won’t automatically do that for you:

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6&
client_id=21302922996.apps.googleusercontent.com&
client_secret=XTHhXh1SlUNgvyWGwDk1EjXB&
redirect_uri=https://www.example.com/back&
grant_type=authorization_code

curl https://accounts.google.com/o/oauth2/token -d
“code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6&client_id=21302922996.apps.googleusercontent.com&client_secret=XTHhXh1SlUNgvyWGwDk1EjXB&redirect_uri=https://www.example.com/back&grant_type=authorization_code
»

What am I doing wrong?
Not receiving a postive reply from the curl

$url = 'https://accounts.google.com/o/oauth2/token?'.urlencode('code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6&client_id=21302922996.apps.googleusercontent.com&client_secret=XTHhXh1SlUNgvyWGwDk1EjXB&redirect_uri=https://www.example.com/back&grant_type=authorization_code');

var_dump(get_web_page($url));

function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

In this line:

$url = 'https://accounts.google.com/o/oauth2/token?'.urlencode('code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp6&client_id=21302922996.apps.googleusercontent.com&client_secret=XTHhXh1SlUNgvyWGwDk1EjXB&redirect_uri=https://www.example.com/back&grant_type=authorization_code');

You are urlencoding the entire query string. You don’t want to do that, you only want to urlencode the values. i.e.

$url = 'https://accounts.google.com/o/oauth2/token';
$url .= '?code='.urlencode('4/P7q7W91a-oMsCeLvIaQm6bTrgtp6');
$url .= '&client_id='.urlencode('21302922996.apps.googleusercontent.com');
$url .= '&client_secret='.urlencode('XTHhXh1SlUNgvyWGwDk1EjXB');
//etc

also, if that is supposed to be submitted via a post request, you need to ONLY use the base url for the curl_init() function, the create the postfields and set that as an option, i.e.:

$url = 'https://accounts.google.com/o/oauth2/token';

$postString = 'code='.urlencode('4/P7q7W91a-oMsCeLvIaQm6bTrgtp6');
$postString .= '&client_id='.urlencode('21302922996.apps.googleusercontent.com');
$postString .= '&client_secret='.urlencode('XTHhXh1SlUNgvyWGwDk1EjXB');
//etc
curl_setopt($request, CURLOPT_POSTFIELDS, $postString);

hope that makes sense