Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with refresh page: Required option not passed: "access_token" #96

Open
jpxwb opened this issue Mar 30, 2024 · 0 comments
Open

Issue with refresh page: Required option not passed: "access_token" #96

jpxwb opened this issue Mar 30, 2024 · 0 comments

Comments

@jpxwb
Copy link

jpxwb commented Mar 30, 2024

Hi - firstly, thank you for making this repository.

I have a simple page below - api-001.php:

<?php
require '../../vendor/autoload.php';

use Strava\API\OAuth;
use Strava\API\Exception;
use Strava\API\Client;
use Strava\API\Service\REST;

try {
    $options = [
        'clientId'     => 123456,
        'clientSecret' => '9876543210',
        'redirectUri'  => 'http://localhost/strava/api-001.php'
    ];
    $oauth = new OAuth($options);

    if (!isset($_GET['code'])) {
        print '<a href="'.$oauth->getAuthorizationUrl([
            // Uncomment required scopes.
            'scope' => [
                'read',
                'read_all',
                'profile:read_all',
                'profile:write',
                'activity:read',
                'activity:read_all',
                'activity:write',
            ]
        ]).'">Connect</a>';
		
    } else {
		
        $token = $oauth->getAccessToken('authorization_code', [
            'code' => $_GET['code']
        ]);
		
        print $token->getToken();
		
		try {
			$adapter = new \GuzzleHttp\Client(['base_uri' => 'https://www.strava.com/api/v3/']);
			$service = new REST($token->getToken(), $adapter);  // Define your user token here.
			$client = new Client($service);
			$athlete = $client->getAthlete();
			print_r($athlete);
			$activities = $client->getAthleteActivities();
		} catch(Exception $e) {
			print $e->getMessage();
		}

    }
} catch(Exception $e) {
    print $e->getMessage();
}
?>

When I first access it, I click Connect and click Authorize.

I'm redirected to http://localhost/strava/api-001.php and can see my athlete info.

However, if I click F5 on the browser, I end up with this error:

PHP Fatal error:  Uncaught InvalidArgumentException: Required option not passed: "access_token" in C:\Data\Websites\vendor\league\oauth2-client\src\Token\AccessToken.php:96
Stack trace:
#0 C:\Data\Websites\vendor\league\oauth2-client\src\Provider\AbstractProvider.php(844): League\OAuth2\Client\Token\AccessToken->__construct(Array)
#1 C:\Data\Websites\vendor\league\oauth2-client\src\Provider\AbstractProvider.php(642): League\OAuth2\Client\Provider\AbstractProvider->createAccessToken(Array, Object(League\OAuth2\Client\Grant\AuthorizationCode))
#2 C:\Data\Websites\strava\api-001.php(33): League\OAuth2\Client\Provider\AbstractProvider->getAccessToken(Object(League\OAuth2\Client\Grant\AuthorizationCode), Array)
#3 {main}
  thrown in C:\Data\Websites\vendor\league\oauth2-client\src\Token\AccessToken.php on line 96

I realise the documentation on the main page for this repository has 2 sections:

First, authorisation and authentication

Then, call your API method!

I tried putting the code for the second part in a separate page, but then it errors on this line:

$service = new REST($token->getToken(), $adapter);  // Define your user token here.

Because I haven't defined $token which is why in my quoted code above I redirect to the same starting page api-001.php as that already has $token defined.

I thought maybe I can store $token in localStorage but I am not sure how I'd store $token in localStorage as it is an array I think, not just a variable with a single value.

Sorry for probably overlooking something very basic - but I cannot see how to set things up so that I can refresh the page, otherwise I have to go back to click Connect each time.

I checked these issues:

#92
#57

But can't work it out.

Sorry if it is something simple, I have basic PHP skills but am trying to work out what to do.

Thanks

@jpxwb jpxwb changed the title Issue with refresh page: Issue with refresh page: Required option not passed: "access_token" Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant