Search code examples
c#androidxamarin.androidfitbitxamarin.auth

Xamarin Android with fitbit api- Add Authorization to header


I have a xamarin android application that is connecting to fitbit through their API and xamarin.auth, I have the access token but am getting an unauthorized code when trying to GET the json response. I have tried to add the access token to the url but it did not work.

This is my code:

if (e.IsAuthenticated)
        {
            var request = new OAuth2Request("GET", new System.Uri("https://api.fitbit.com/1/user/-/profile.json?access_token=" + e.Account.Properties["access_token"]), null, e.Account);
            request.AccessTokenParameterName = e.Account.Properties["access_token"];

            string type = e.Account.Properties["token_type"];
            var response = await request.GetResponseAsync();
            var json = response.GetResponseText();
        }

I can not figure out how to add the authorization to the header of the OAuth2Request.

Any help is much appreciated!


Solution

  • So I ended up adding a Custom Request class to extend the OAuth2Request class and added another GetResponseAsync function that takes the access token as a parameter. I then add the Authorization to the header and leave the rest of the function as is. Not sure if there's a way of doing this with Xamarin.Auth library the way it is, but it worked for me.

    public class CustomRequest : OAuth2Request{ //..
        public virtual async Task<Response> GetResponseAsync(string accessToken){//..
        httpRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);