Skip to main content

I'm trying to authorize myself to use the strava API. I managed to get an access token and specifying the scopes. In order to be able to write data to one of my activities I need to pass in an authorization code (with the great help of ​@Jan_Mantau).  On the internet I found examples of getting the url that would display the code.  

 

static async Task Main(strings] args)
{
// Step 1: Send user to authorization page
var queryParams = new Dictionary<string, string>
{
{ "client_id", clientId },
{ "response_type", "code" },
{ "redirect_uri", redirectUri },
{ "scope", scope }
};
var authorizeUri = $"{authorizeUrl}?{string.Join("&", queryParams.Select(kvp => $"{kvp.Key}={Uri.EscapeDataString(kvp.Value)}"))}";
Console.WriteLine($"Open the authorization url: {authorizeUri}");

// Step 2: User authorizes on page and get redirected to the redirect)uri with a code
...

}

 https://www.strava.com/oauth/authorize?client_id=99999999&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2Ftoken_exchange&scope=activity%3Aread_all%2Cactivity%3Awrite

 

using this url in a browser, gives me the authorization page on which I click approve after which I get redirected to the localhost url specified.  In that localhost url the code that I need is displayed in the querystring….  

In my use case I want to receive the authorization code in my code so I can use it to do the actual write on the activity.   I know I have to authorize it one time manually. 
How can I get the authorization code in code  by calling the authorizeUri?   For development purposes I'm writing code that runs on a laptop. In a later stage I'll turn my code into a mobile app.

 

 

 

 

 

 

 

 

FYI - this is how I get my token


public StravaTokenResponse exchangeCodeForToken(String code) {

log.debug("Start exchangeCodeForToken");

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
body.add("client_id", stravaConfig.getClientId());
body.add("client_secret", stravaConfig.getClientSecret());
body.add("code", code); // from mobile app
body.add("grant_type", "authorization_code");


HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body, headers);
log.debug("exchangeCodeForToken request:" + request);

try {
ResponseEntity<StravaTokenResponse> response = restTemplate.postForEntity(
stravaConfig.getTokenUri(),
request,
StravaTokenResponse.class
);
if (response == null) throw new RuntimeException("Strava returned null");
log.debug("Received exchangeCodeForToken getStatusCode :" + response.getStatusCode());
log.debug("Received exchangeCodeForToken response :" + response.getBody());
if (storeToken(response.getBody())) {
log.debug("Token stored");
// Save was successful
// TODO: what do we do if not stored successfully?
}
return response.getBody();
} catch (RestClientException e) {
log.debug("Received exchangeCodeForToken RestClientException :" + e.getMessage());
throw new RuntimeException(e);
}
}

 


The full documentation for authentication is here - https://developers.strava.com/docs/authentication/. It has an example you can walk through where you visit the generated auth URI, then use cURL to exchange the code for a token. If you’re implementing things yourself, I recommend going through the example manually, then translating it into code which does it automatically. That way you understand the process and what needs to be done.

 

Alternatively, you can use an OAuth2 library for whatever programming language(s) you’re using. Check here - https://oauth.net/code/


Reply