Gonkas
Gonkas

Reputation: 103

How to get reviews from Trustpilot API

Attempting to get Trustpilot reviews from a specific product but cannot get anywhere.

Can get the token with success but then the reviews:

AND

The ApiKey is the same to get the token so do not understand why does not work here. In a desperate attempt added the token to the URL (?token={token}) and also not successful.

The sku collection includes all variants. Also tried without sku parameter and nothing is retrieved.

Here is the code:

var ApiKey = "ApiKey";
var SecretKey = "SecretKey";
var Username = "Username";
var Password = "Password";

var serverUrl = "xxx.xxx.xxx.xxx";

var BusinnessUnit = "BusinnessUnit";

var AuthUrl2 = "https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken";
var ReviewUrl = "https://api.trustpilot.com/v1/private/product-reviews/business-units/"+ bkBusinnessUnit + "/reviews";
var ProductReviewUrl = "https://api.trustpilot.com/v1/product-reviews/business-units/" + bkBusinnessUnit + "/reviews";

var token = "";

// get token
using (var httpClient = new HttpClient())
{
    try
    {
        httpClient.BaseAddress = new Uri(AuthUrl2);

        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

        var authString = ApiKey + ":" + SecretKey;
        string encodedStr = Convert.ToBase64String(Encoding.UTF8.GetBytes(authString));
        httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", encodedStr);

        var stringPayload = "grant_type=password&username=" + Username + "&password=" + Password;

        var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/x-www-form-urlencoded");
        HttpResponseMessage httpResponseMessage = httpClient.PostAsync(AuthUrl2, httpContent).Result;

        var accessTokenResponseString = httpResponseMessage.Content.ReadAsStringAsync().Result;

        var accessTokenResponseObject = JsonConvert.DeserializeObject<AccessTokenResponse>(accessTokenResponseString);

        token = accessTokenResponseObject.AccessToken;
        Console.WriteLine("Token retrieved. " + token);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed!" + ex.ToString());
    }
}

// get product reviews
using (var httpClient = new HttpClient())
{
    try
    {
        httpClient.BaseAddress = new Uri(ProductReviewUrl);

        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

        var authString = ApiKey + ":" + SecretKey;
        string encodedStr = Convert.ToBase64String(Encoding.UTF8.GetBytes(authString));
        httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", encodedStr);

        var stringPayload = "sku=SK40132-BLK-6,SK40132-BLK-8,SK40132-BLK-10,SK40132-BLK-12,SK40132-BLK-14,SK40132-BLK-16,SK40132-BLK-18&token=" + token;

        var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/x-www-form-urlencoded");
        HttpResponseMessage httpResponseMessage = httpClient.PostAsync(ProductReviewUrl, httpContent).Result;

        var reviewResponseString = httpResponseMessage.Content.ReadAsStringAsync().Result;

        var reviewResponseObject = JsonConvert.DeserializeObject<AccessTokenResponse>(reviewResponseString);

        Console.WriteLine(reviewResponseObject + "Product review retrieved." + reviewResponseString);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed!" + ex.ToString());
    }
}

// get reviews
using (var httpClient = new HttpClient())
{
    try
    {
        httpClient.BaseAddress = new Uri(ReviewUrl);

        httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

        httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

        var stringPayload = "sku=SK40132-BLK-6,SK40132-BLK-8,SK40132-BLK-10,SK40132-BLK-12,SK40132-BLK-14,SK40132-BLK-16,SK40132-BLK-18";

        var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/x-www-form-urlencoded");
        HttpResponseMessage httpResponseMessage = httpClient.PostAsync(ReviewUrl, httpContent).Result;

        var reviewResponseString = httpResponseMessage.Content.ReadAsStringAsync().Result;

        var reviewResponseObject = JsonConvert.DeserializeObject<AccessTokenResponse>(reviewResponseString);

        Console.WriteLine(reviewResponseObject + "Review retrieved." + reviewResponseString);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Failed!" + ex.ToString());
    }
}

Upvotes: 0

Views: 5016

Answers (2)

Gonkas
Gonkas

Reputation: 103

It was incredible simple it should be a GetAsync instead of a PostAsync.

Thanks anyway for looking at it.

Upvotes: 0

Arunas Stonis
Arunas Stonis

Reputation: 179

/v1/private/product-reviews/business-units/{businessUnitId}/reviews endpoint requires token. It could be provided either via header or via query parameters. It works for you as you did it right.

/v1/product-reviews/business-units/{businessUnitId}/reviews endpoint only requires apikey. You could pass it in two ways:

  • via header

    httpClient.DefaultRequestHeaders.Accept.Add("apikey", ApiKey);

  • via query parameters

    var stringPayload = "sku=SK40132-BLK-6,SK40132-BLK-8,SK40132-BLK-10,SK40132-BLK-12,SK40132-BLK-14,SK40132-BLK-16,SK40132-BLK-18&apikey=" + ApiKey;

Upvotes: 1

Related Questions