Community Spring Cleaning week is here! Join your fellow Maveryx in digging through your old posts and marking comments on them as solved. Learn more here!

Alteryx IO Discussions

Customize and extend the power of Alteryx with SDKs, APIs, custom tools, and more.

access v3 APIs from c#

mbenne
5 - Atom

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?

 

https://help.alteryx.com/developer-help/server-api-overview

4 REPLIES 4
PetrT
Alteryx
Alteryx

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.

mbenne
5 - Atom

yes, please.

PetrT
Alteryx
Alteryx

It is part of our metadata loader app, so it is not supposed to run as is, but it could be something you can take an inspiration from.

mkhtran
8 - Asteroid

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.