C# -- Login with Amazon oauth2 -- error when doing POST-request

672 Views Asked by At

I am trying to make a POST request to the amazon api to get a token for login with amazon. I am doing this with Xamarin.

What I want is the response from amazon with the token. I tried it according to this tutorial:

Code-Based Linking (CBL) for Amazon Alexa Devices

When using Postman it works fine, I get the right response. When I try to implement this in my Xamarin project I always get the error:

"error_description":"The Content-type is not supported by the authorization server.", "error":"invalid_request"

Here's my code:

client = new HttpClient();
client.BaseAddress = new Uri("https://api.amazon.com/auth/O2/create/codepair");

string contentRaw = "response_type=device_code&client_id=hereIsTheClient_ID&scope=alexa:all";
var content = new StringContent(contentRaw, Encoding.UTF8, "application/x-www-form-urlencoded");
string uriEncoded = Uri.EscapeDataString(contentRaw);
HttpResponseMessage response = await client.PostAsync("https://api.amazon.com/auth/O2/create/codepair", content);
var result = await response.Content.ReadAsStringAsync();
return result;

Here's what I did using JSON, but I still get the "Content-Type is not supported by the authorization server"

 O2authRequest o2AuthRequest = new O2authRequest();
 o2AuthRequest.response_type = "device_code";
 o2AuthRequest.client_id = "amzn1.application-oa2-client....";
 o2AuthRequest.scope = "alexa:all";
 o2AuthRequest.scope_data = new Scope_Data(); 
 o2AuthRequest.scope_data.productID = "MyAppID";
 o2AuthRequest.scope_data.productInstanceAttributes = new Productinstanceattributes();
 o2AuthRequest.scope_data.productInstanceAttributes.deviceSerialNumber = "12345";
 string contentRaw = JsonConvert.SerializeObject(o2AuthRequest);

 var content = new StringContent(contentRaw, Encoding.UTF8, "application/json");
 string uriEncoded = Uri.EscapeDataString(contentRaw);

 HttpResponseMessage response = await client.PostAsync("https://api.amazon.com/auth/O2/create/codepair", content);
 var result = await response.Content.ReadAsStringAsync();
0

There are 0 best solutions below