C#-使用Amazon oauth2登录-执行POST请求时出错

时间:2018-09-25 09:18:02

标签: c# xamarin alexa-voice-service login-with-amazon

我正在尝试向亚马逊api发出POST请求,以获取用于登录亚马逊的令牌。我正在使用Xamarin。

我想要的是来自亚马逊的带有令牌的响应。我根据本教程尝试过:

Code-Based Linking (CBL) for Amazon Alexa Devices

使用Postman时,效果很好,我得到了正确的答复。当我尝试在Xamarin项目中实现此功能时,总是会收到错误消息:

“ error_description”:“授权服务器不支持Content-type。”,“ error”:“ invalid_request”

这是我的代码:

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;

这是我使用JSON所做的事情,但是我仍然看到“授权服务器不支持Content-Type”

 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 个答案:

没有答案