Variables

Variables

Need to read a Domo dataset using C#

I am having trouble getting an access token to work in C#. I am writing an application, and I need to update an office list using the data in one of my Domo datasets.

I have created client and secret IDs and post to "https://api.domo.com/oauth/token". I have tried several grant types and included/excluded a scope. The response is always unauthorized.

StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1,…


  1. var tokenUrl = "https://api.domo.com/oauth/token";
  2. var values = new Dictionary<string, string>
  3. {
  4. { "grant_type", "client_credentials" },
  5. { "scope", "data" },
  6. { "client_id", clientId },
  7. { "client_secret", clientSecret }
  8. };

  9. var content = new FormUrlEncodedContent(values);



  10. HttpResponseMessage response = client.PostAsync(tokenUrl, content).Result;
  11. if (!response.IsSuccessStatusCode)
  12. {
  13. throw new Exception($"Failed to get access token: {response.ReasonPhrase}");
  14. }

I just need to read a single dataset in Domo, using C#. Or VB.net…I'll convert code if needed. I haven't had trouble using Python. But that doesn't translate to C#.

** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **

Best Answer

  • Coach
    Answer ✓

    It looks like you're not passing in an Authorization Header.

    Here's some sample code I've used in the past which may be helpful:

    1. using System;
    2. using System.Net.Http;
    3. using System.Net.Http.Headers;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6.  
    7. class Program
    8. {
    9. private static readonly string ClientId = "YOUR_CLIENT_ID";
    10. private static readonly string ClientSecret = "YOUR_CLIENT_SECRET";
    11. private static readonly string DomoAuthUrl = "https://api.domo.com/oauth/token";
    12. static async Task Main(string[] args)
    13. {
    14. string token = await GetDomoOAuthToken();
    15. Console.WriteLine($"OAuth Token: {token}");
    16. }
    17.  
    18. private static async Task<string> GetDomoOAuthToken()
    19. {
    20. using (HttpClient client = new HttpClient())
    21. {
    22. // Construct the authorization header
    23. string authHeader = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ClientId}:{ClientSecret}"));
    24. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeader);
    25.  
    26. // Prepare the request content
    27. HttpContent content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");
    28.  
    29. // Make the POST request
    30. HttpResponseMessage response = await client.PostAsync(DomoAuthUrl, content);
    31.  
    32. // Ensure the request was successful
    33. response.EnsureSuccessStatusCode();
    34.  
    35. // Parse and return the token from the response
    36. string responseContent = await response.Content.ReadAsStringAsync();
    37. dynamic jsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(responseContent);
    38. return jsonResponse.access_token;
    39. }
    40. }
    41. }
    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**

Answers

  • Coach
    Answer ✓

    It looks like you're not passing in an Authorization Header.

    Here's some sample code I've used in the past which may be helpful:

    1. using System;
    2. using System.Net.Http;
    3. using System.Net.Http.Headers;
    4. using System.Text;
    5. using System.Threading.Tasks;
    6.  
    7. class Program
    8. {
    9. private static readonly string ClientId = "YOUR_CLIENT_ID";
    10. private static readonly string ClientSecret = "YOUR_CLIENT_SECRET";
    11. private static readonly string DomoAuthUrl = "https://api.domo.com/oauth/token";
    12. static async Task Main(string[] args)
    13. {
    14. string token = await GetDomoOAuthToken();
    15. Console.WriteLine($"OAuth Token: {token}");
    16. }
    17.  
    18. private static async Task<string> GetDomoOAuthToken()
    19. {
    20. using (HttpClient client = new HttpClient())
    21. {
    22. // Construct the authorization header
    23. string authHeader = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ClientId}:{ClientSecret}"));
    24. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeader);
    25.  
    26. // Prepare the request content
    27. HttpContent content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");
    28.  
    29. // Make the POST request
    30. HttpResponseMessage response = await client.PostAsync(DomoAuthUrl, content);
    31.  
    32. // Ensure the request was successful
    33. response.EnsureSuccessStatusCode();
    34.  
    35. // Parse and return the token from the response
    36. string responseContent = await response.Content.ReadAsStringAsync();
    37. dynamic jsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(responseContent);
    38. return jsonResponse.access_token;
    39. }
    40. }
    41. }
    **Was this post helpful? Click Agree or Like below**
    **Did this solve your problem? Accept it as a solution!**
  • That works. Thank you @GrantSmith .

    ** Was this post helpful? Click Agree or Like below. **
    ** Did this solve your problem? Accept it as a solution! **

Welcome!

It looks like you're new here. Members get access to exclusive content, events, rewards, and more. Sign in or register to get started.
Sign In