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,…
- var tokenUrl = "https://api.domo.com/oauth/token";
- var values = new Dictionary<string, string>
- {
- { "grant_type", "client_credentials" },
- { "scope", "data" },
- { "client_id", clientId },
- { "client_secret", clientSecret }
- };
- var content = new FormUrlEncodedContent(values);
- HttpResponseMessage response = client.PostAsync(tokenUrl, content).Result;
- if (!response.IsSuccessStatusCode)
- {
- throw new Exception($"Failed to get access token: {response.ReasonPhrase}");
- }
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
-
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:
- using System;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Threading.Tasks;
- class Program
- {
- private static readonly string ClientId = "YOUR_CLIENT_ID";
- private static readonly string ClientSecret = "YOUR_CLIENT_SECRET";
- private static readonly string DomoAuthUrl = "https://api.domo.com/oauth/token";
- static async Task Main(string[] args)
- {
- string token = await GetDomoOAuthToken();
- Console.WriteLine($"OAuth Token: {token}");
- }
- private static async Task<string> GetDomoOAuthToken()
- {
- using (HttpClient client = new HttpClient())
- {
- // Construct the authorization header
- string authHeader = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ClientId}:{ClientSecret}"));
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeader);
- // Prepare the request content
- HttpContent content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");
- // Make the POST request
- HttpResponseMessage response = await client.PostAsync(DomoAuthUrl, content);
- // Ensure the request was successful
- response.EnsureSuccessStatusCode();
- // Parse and return the token from the response
- string responseContent = await response.Content.ReadAsStringAsync();
- dynamic jsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(responseContent);
- return jsonResponse.access_token;
- }
- }
- }
**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**0
Answers
-
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:
- using System;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Threading.Tasks;
- class Program
- {
- private static readonly string ClientId = "YOUR_CLIENT_ID";
- private static readonly string ClientSecret = "YOUR_CLIENT_SECRET";
- private static readonly string DomoAuthUrl = "https://api.domo.com/oauth/token";
- static async Task Main(string[] args)
- {
- string token = await GetDomoOAuthToken();
- Console.WriteLine($"OAuth Token: {token}");
- }
- private static async Task<string> GetDomoOAuthToken()
- {
- using (HttpClient client = new HttpClient())
- {
- // Construct the authorization header
- string authHeader = Convert.ToBase64String(Encoding.ASCII.GetBytes($"{ClientId}:{ClientSecret}"));
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeader);
- // Prepare the request content
- HttpContent content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");
- // Make the POST request
- HttpResponseMessage response = await client.PostAsync(DomoAuthUrl, content);
- // Ensure the request was successful
- response.EnsureSuccessStatusCode();
- // Parse and return the token from the response
- string responseContent = await response.Content.ReadAsStringAsync();
- dynamic jsonResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(responseContent);
- return jsonResponse.access_token;
- }
- }
- }
**Was this post helpful? Click Agree or Like below**
**Did this solve your problem? Accept it as a solution!**0 -
That works. Thank you @GrantSmith .
** Was this post helpful? Click Agree or Like below. **
** Did this solve your problem? Accept it as a solution! **0
Categories
- All Categories
- 2K Product Ideas
- 2K Ideas Exchange
- 1.6K Connect
- 1.3K Connectors
- 310 Workbench
- 7 Cloud Amplifier
- 9 Federated
- 3K Transform
- 113 SQL DataFlows
- 654 Datasets
- 2.2K Magic ETL
- 4K Visualize
- 2.5K Charting
- 796 Beast Mode
- 78 App Studio
- 44 Variables
- 757 Automate
- 188 Apps
- 480 APIs & Domo Developer
- 72 Workflows
- 17 DomoAI
- 40 Predict
- 17 Jupyter Workspaces
- 23 R & Python Tiles
- 408 Distribute
- 119 Domo Everywhere
- 279 Scheduled Reports
- 10 Software Integrations
- 141 Manage
- 137 Governance & Security
- 8 Domo Community Gallery
- 47 Product Releases
- 12 Domo University
- 5.4K Community Forums
- 41 Getting Started
- 31 Community Member Introductions
- 114 Community Announcements
- 4.8K Archive