basvandorst/StravaPHP

Having trouble getting OAuth2 to work and missing refresh token

phpfui opened this issue · 7 comments

I have used OAuth2 before, but having trouble getting this library to work as documented. And I don't see how you get a refresh token. It can probably can all be solved with more complete examples.

Right now I am getting the following error:
Required option not passed: "access_token" /League/OAuth2/Client/Token/AccessToken.php 96
called from /League/OAuth2/Client/Provider/AbstractProvider.php 746
called from /League/OAuth2/Client/Provider/AbstractProvider.php 544

I don't see "access_token" in the documentation.

I appreciate any insights.

Hi there, it would be useful to see your code examples in your implementation to gain a clearer understanding of what you are doing. Please could you provide these

In trying to get a token, I am doing this:

$client = new \Strava\API\OAuth([
	'clientId' => $clientId,
	'clientSecret' => $token,
	'redirectUri' => $myRedirectUrl, ]);

\header('location: ' . $client->getAuthorizationUrl(['scope' => ['read', ]]));

Then on the redirect page:

$token = $client->getAccessToken('authorization_code', ['code' => $_GET['code']]);
$bearerToken = $token->getToken();
$refreshToken = $token->getRefreshToken();

One question is how do I refresh the token?

As far as I can tell, the above code should work - this is exactly how I access the tokens.

Your error seems to be coming from the AccessToken constructor. Have the checked the response you are getting from Strava?

One question is how do I refresh the token?

@phpfui you can call the following:

$newAccessToken = $client->getAccessToken('refresh_token', [
    'refresh_token' => $oldAccessToken->getRefreshToken(),
]);

Where $client in an instance of Strava\API\OAuth (it is probably better to name this $oauth to prevent confusion with Strava\API\Client).

Thanks for getting back to me. Turns out the Strava API will not get me what I want, which is mileage and elevation of any public activity. The code was trivial once I looked at the HTML, which Strava has conveniently structured well.

<?php

namespace App\Model;

class Strava
	{
	private function __construct(private array $data)
		{
		}

	public function getMileage() : float
		{
		return (float)$this->data['mi'] ?? 0;
		}

	public function getElevationFeet() : int
		{
		return (int)$this->data['ft'] ?? 0;
		}

	public static loadFromActivityId(string $activityId) : ?\App\Model\Strava
		{
		$url = 'https://www.strava.com/activities/' . $activityId;
		$html = file_get_contents($url);
		if (! $html)
			{
			return null;
			}
		$dom = new \voku\helper\HtmlDomParser($html);

		$parts = [];
		foreach ($dom->find('.unit') as $node)
			{
			$unit = $node->text();
			$value = str_replace([' ' . $unit, ','], '', $node->parentNode()->text());
			$parts[$unit] = $value;
			}

		return new \App\Model\Strava($parts);
		}
	}

I am only using this in the US, so miles and feet, but I imagine it would be easy to also do the metric pages as well.

Thanks again for your library and support.

@phpfui Does this issue still require support or has it been resolved?

Turns out I can't use the API for my issue anyway. Closing. Thanks for the feedback.