Exchanging Auth Code for Tokens

Discussion in '[bd] API' started by Warchamp7, Jan 10, 2014.

  1. Warchamp7

    Warchamp7 New Member

    I'm trying to set things up on a test server at the moment and running into problems at this stage. This is my callback script after authorizing on XenForo.

    Code:
    
    $url = 'http://url.com/forum/api/index.php?oauth/token';
    
    $vars = 'code=' . $authCode .
    '&client_id=' . $consKey .
    '&client_secret=' . $consSecret .
    '&redirect_url=' . $redirectURI .
    '&grant_type=authorization_code';
    
    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_POST, 1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $vars);
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt( $ch, CURLOPT_HEADER, 0);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
    
    $response = curl_exec( $ch );
    
    print_r($response);
    
    The response I get is
    {"error":"invalid_client"}

    This is my first time really working with something like this, so all help is appreciated.
     
  2. xfrocks

    xfrocks XenForo rocks!
    Staff Member

    If you got "invalid_client", that means your client id/secret combination is not correct. Please double check it?
     
  3. Warchamp7

    Warchamp7 New Member

    Oh my god, I feel so foolish, I had a typo.

    However, we're not out of the forest yet, now I'm getting 'invalid_request'
     
    1. xfrocks

      xfrocks XenForo rocks!
      Staff Member

      xfrocks @Warchamp7 That's some progress :D

      I think you have another typo: "redirect_url" should be "redirect_uri" ;)
       
    2. Warchamp7

      Warchamp7 New Member

      Warchamp7 @xfrocks Ahh, it seems it's one of those nights x.x Perhaps I should take a break for a bit :p

      More progress! I'm now getting {"error":"invalid_grant"}, where is the typo now ;_;
       
    3. xfrocks

      xfrocks XenForo rocks!
      Staff Member

      xfrocks @Warchamp7 You have reached this far so it's not a typo this time I think. There are 2 causes for this:
      1. You should make sure the $authCode is correct. You get it from a redirect after the authorization, right?
      2. The $redirectURI should match the one when you requested authorization. This is the common cause ;)
       
  4. Warchamp7

    Warchamp7 New Member

    It was the latter. My response has my access/refresh tokens now. Thank you very much!
     
    1. xfrocks

      xfrocks XenForo rocks!
      Staff Member

      xfrocks @Warchamp7 I knew it :D Congrats on your work. Now take some rest!
       
Loading...