Skip to main content
Question

Beginner question: How to get the authorizationcode programmatically?


Forum|alt.badge.img+1

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(string[] 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.

 

 

 

 

 

 

 

 

0 replies

Be the first to reply!

Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept, you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings