iamstuartwilson / strava

PHP Class for the Strava API (v3)
MIT License
120 stars 26 forks source link

Cannot get properly acccess token #28

Closed zcampaner closed 6 years ago

zcampaner commented 7 years ago

I am getting this kind of errror stdClass::__set_state(array( 'message' => 'Authorization Error', 'errors' => array ( 0 => stdClass::__set_state(array( 'resource' => 'Application', 'field' => '', 'code' => 'invalid', )), ), ))

And this is my script

`$api = new Iamstuartwilson\StravaApi( $this->client_id, $this->client_secret ); $redirect = 'http://www.onlineraces.ph/app/Admin/auth_login';

    $code = $this->uri->segment(3);

    $accessToken = $api->tokenExchange($code);
    $api->setAccessToken($accessToken);

    $athleteResponse = $api->get( 'athlete');
    var_export($accessToken);`
zcampaner commented 7 years ago

Hoping to get fast feedback and help as I am in the midst of a deadline :(

adriangibbons commented 7 years ago

Assuming $this->client_id, $this->client_secret and $code are valid then try:

$results = $api->tokenExchange($code);
$api->setAccessToken($results->access_token);

$code should be in the URL that Strava redirects you back to as part of the Oauth handshake. I use something like:

$code = $_GET['code'];  // Or $code = filter_input(INPUT_GET, 'code', FILTER_SANITIZE_STRING);
zcampaner commented 7 years ago

I still got the same errors

object(stdClass)#21 (2) { ["message"]=> string(19) "Authorization Error" ["errors"]=> array(1) { [0]=> object(stdClass)#22 (3) { ["resource"]=> string(11) "Application" ["field"]=> string(0) "" ["code"]=> string(7) "invalid" } }

adriangibbons commented 7 years ago

If you do a var_dump() for:

$this->client_id
$this->client_secret
$code
$api
$results

Are they all set and valid?

iamstuartwilson commented 7 years ago

@zcampaner did you manage to get this working? So sorry, I didn't spot this issue earlier.

From the looks of the error, it might be that the $code you're passing to $api->tokenExchange($code); is not valid. Are you going through the process of generating the authentication URL and coming back to the redirect page with the ?code param populated before making the token exchange?

@adriangibbons thanks a lot for helping. Really appreciate it 👍

iamstuartwilson commented 6 years ago

Closing due to inactivity.