1

I am trying to use the Trustpilot API, to post invitations to review products.

I have successfully gone through the authentication step as you can see in the code below, however I am unable to successfully post data to the Trustpilot Invitations API. The PostAsnyc method appears to be stuck with an WaitingForActivation status. I wonder if there is anything you can suggest to help.

Here is my code for this (the API credentials here aren't genuine!):

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());
}
Martin Buberl
  • 45,844
  • 25
  • 100
  • 144
t_plusplus
  • 4,079
  • 5
  • 45
  • 60

1 Answers1

3

This is how I solved the issue:

    // 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();

This question here on SO was helpful: How do you set the Content-Type header for an HttpClient request?

Community
  • 1
  • 1
t_plusplus
  • 4,079
  • 5
  • 45
  • 60