SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    PHP Code:
    $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;


  2. #2
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In this line:
    PHP Code:
    $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.
    PHP Code:
    $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 
    aaron-fisher.com - PHP articles and more

  3. #3
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.:

    PHP Code:
    $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($requestCURLOPT_POSTFIELDS$postString); 
    hope that makes sense
    aaron-fisher.com - PHP articles and more


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •