Trustpilot OAuth Restful API:无法 PostAsync
Posted
技术标签:
【中文标题】Trustpilot OAuth Restful API:无法 PostAsync【英文标题】:Trustpilot OAuth Restful API: Unable to PostAsync 【发布时间】:2016-10-14 03:30:43 【问题描述】:我正在尝试使用Trustpilot API 发布产品评论邀请。
正如您在下面的代码中看到的那样,我已成功完成身份验证步骤,但是我无法成功将数据发布到Trustpilot Invitations API。 PostAsnyc 方法似乎卡在 WaitingForActivation 状态。我想知道您是否有什么可以提供帮助的建议。
这是我的代码(这里的 API 凭据不是真实的!):
using (HttpClient httpClient = new HttpClient())
string trustPilotAccessTokenUrl = "https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken";
httpClient.BaseAddress = new Uri(trustPilotAccessTokenUrl);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
var authString = "MyApiKey:MyApiSecret";
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Base64Encode(authString));
var stringPayload = "grant_type=password&username=MyUserEmail&password=MyPassword";
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/x-www-form-urlencoded");
HttpResponseMessage httpResponseMessage = httpClient.PostAsync(trustPilotAccessTokenUrl, httpContent).Result;
var accessTokenResponseString = httpResponseMessage.Content.ReadAsStringAsync().Result;
var accessTokenResponseObject = JsonConvert.DeserializeObject<AccessTokenResponse>(accessTokenResponseString);
// Create invitation object
var invitation = new ReviewInvitation
ReferenceID = "inv001",
RecipientName = "Jon Doe",
RecipientEmail = "Jon.Doe@comp.com",
Locale = "en-US"
;
var jsonInvitation = JsonConvert.SerializeObject(invitation);
var client = new HttpClient();
client.DefaultRequestHeaders.Add("token", accessTokenResponseObject.AccessToken);
var invitationsUri = new Uri("https://invitations-api.trustpilot.com/v1/private/business-units/MyBusinessID/invitations");
// This here as a status of WaitingForActivation!
var a = client.PostAsync(invitationsUri, new StringContent(jsonInvitation)).ContinueWith((postTask) => postTask.Result.EnsureSuccessStatusCode());
【问题讨论】:
【参考方案1】:这就是我解决问题的方法:
// Serialize our concrete class into a JSON String
var jsonInvitation = JsonConvert.SerializeObject(invitationObject);
// Wrap our JSON inside a StringContent which then can be used by the HttpClient class
var stringContent = new StringContent(jsonInvitation);
// Get the access token
var token = GetAccessToken().AccessToken;
// Create a Uri
var postUri = new Uri("https://invitations-api.trustpilot.com/v1/private/business-units/BusinessUnitID/invitations");
// Set up the request
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, postUri);
request.Content = stringContent;
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
request.Content.Headers.Add("token", token);
// Set up the HttpClient
var httpClient = new HttpClient();
//httpClient.DefaultRequestHeaders.Accept.Clear();
//httpClient.BaseAddress = postUri;
//httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
//httpClient.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-US"));
var task = httpClient.SendAsync(request);
task.Wait();
这个关于 SO 的问题很有帮助: How do you set the Content-Type header for an HttpClient request?
【讨论】:
以上是关于Trustpilot OAuth Restful API:无法 PostAsync的主要内容,如果未能解决你的问题,请参考以下文章
angularjs:restful api 的 oauth2 实现
如何在 RestFul 应用程序中使用 OAuth 2 在前端处理用户权限?
使用Spring Security和OAuth2实现RESTful服务安全认证