access v3 APIs from c#
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I downloaded sample C# project available at below URL. it is working with v1 apis, but when I change to v3 api its erroring out, tried multiple ways but unable to access. can someone share a sample for v3 and v2 versions?
- Labels:
-
API
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Hey @mbenne , I'm not sure how much you changed to v3 API endpoints. As stated on the help page you mentioned, V3 API endpoints use different authentication method - OAuth2. Sample C# project uses OAuth1 to authenticate V1 API calls. OAuth2 authentication for V3 endpoints is quite easy to implement, but as far as I know there is no sample C# project available. You should follow this help page https://help.alteryx.com/20214/server/server-api-configuration-and-authorization. If you are interested, I can share Python implementation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
yes, please.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
I was able to get this working with `kiota`
kiota generate -d "{SERVER_URL}/webapi//swagger/docs/3" -o ".\api-v3" -l CSharp
As for the `IAccessTokenProvider`,
public async Task<string> GetToken(bool invalidateCache = false)
{
if (invalidateCache || this.expiresAt < DateTime.Now)
{
this.accessToken = "";
this.expiresAt = DateTime.MinValue;
string body;
using HttpRequestMessage request = new(HttpMethod.Post, new Uri(this.baseUri, "oauth2/token"))
{
Content = new StringContent("grant_type=client_credentials")
};
request.Content.Headers.ContentType = new("application/x-www-form-urlencoded");
string authPlain = $"{this.apiKey}:{this.apiSecret}";
byte[] authBytes = Encoding.GetEncoding("ISO-8859-1").GetBytes(authPlain);
string authEncoded = Convert.ToBase64String(authBytes);
request.Headers.Add("Authorization", $"Basic {authEncoded}");
using HttpResponseMessage response = await this.httpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
body = await response.Content.ReadAsStringAsync();
string accessToken;
string tokenType;
int expiresIn;
try
{
JsonNode root = JsonNode.Parse(body)
?? throw new JsonException("Body is null");
accessToken = root["access_token"].GetValue<string>();
tokenType = root["token_type"].GetValue<string>();
expiresIn = root["expires_in"].GetValue<int>();
}
catch (JsonException)
{
throw;
}
catch (Exception ex)
{
throw new JsonException($"Error parsing JSON: {ex.Message}");
}
if (tokenType != "bearer")
throw new NotSupportedException($"Unexpected token type: {tokenType}");
this.accessToken = accessToken;
this.expiresAt = DateTime.Now.AddSeconds(expiresIn - 10);
}
return this.accessToken;
}
And this works wonderfully for v1 and v2 as well.