Anyone programming in .Net?

(Gus Landt) #1

I just got access and I am beginning programming an interface between my store and TCGPlayer. I’m programming in ASP.Net C#. I was wondering if anyone else has done this, and if you had any advice for me.

Sadly I am hung up on the very first step - how to post a call to get my bearer_token passing in my keys as data-binary. .Net doesn’t seem to have built in functions to pass data that way. I’m using an HttpWebRequest to make these calls.

(Joshua Burdick) #2

We have a pretty extensive ASP.Net C# library that I’m looking to get published here in not to long for interacting with the API. That doesn’t help you right now, though, so here’s an excerpt for authenticating:

             client = new HttpClient();
             client.DefaultRequestHeaders
                    .Accept
                    .Add(new MediaTypeWithQualityHeaderValue("application/json")); //ACCEPT header
             client.DefaultRequestHeaders.Add("X-Tcg-Access-Token", AuthKey);

            StringContent postData =
                new StringContent(
                    $"grant_type=client_credentials&client_id={PublicKey}&client_secret={PrivateKey}",
                    Encoding.UTF8,
                    "application/json");

            var result = await client.PostAsync(url, postData).ConfigureAwait(false);

            if (result.IsSuccessStatusCode)
            {
                var respData = await result.Content.ReadAsStringAsync();
                JsonSerializerSettings settings = new JsonSerializerSettings();
                settings.MissingMemberHandling = MissingMemberHandling.Error;

                AccessToken = JsonConvert.DeserializeObject<TcgAccessToken>(respData, settings);
                AccessToken.SaveToCache();
            }
            else
            {
                throw new TcgApiException()
                {
                    StatusCode = result.StatusCode,
                    Headers = result.Headers,
                    ReasonPhrase = result.ReasonPhrase,
                    RequestMessage = result.RequestMessage,
                    IsSuccessStatusCode = result.IsSuccessStatusCode
                };
            }
(Gus Landt) #3

I really appreciate the assist! It would be awesome to have those libraries. I’ll just keep muddling through in the meanwhile. :slight_smile:

Any chance you could share the definitions for TcgAccessToken & TcgApiException?

1 Like
(Joshua Burdick) #4

Exception:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace TcgPlayerApi
{
    public class TcgApiException : Exception
    {
        public HttpStatusCode StatusCode;
        public HttpResponseHeaders Headers;
        public string ReasonPhrase;
        public HttpRequestMessage RequestMessage;
        public bool IsSuccessStatusCode;

        public override string ToString()
        {
            return
                $"Code: {StatusCode}\nReason: {ReasonPhrase}\nRequest: {RequestMessage.ToString()}\nHeaders: {Headers.ToString()}";
        }
    }
}

Token:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace TcgPlayerApi
{
    public class TcgAccessToken : TcgObject
    {
        [JsonProperty("access_token")]
        public string accessToken;
        [JsonProperty("token_type")]
        public string tokenType;
        [JsonProperty("expires_in")]
        public long expiresIn;
        [JsonProperty("userName")]
        public string userName;
        [JsonProperty(".issued")]
        public DateTime issued;
        [JsonProperty(".expires")]
        public DateTime expires;

        protected override string GetObjectId()
        {
            return userName;
        }
    }
}
1 Like
(Gus Landt) #5

You are amazing! Any chance I could also get the definition of the base TcgObject that TcgAccessToken inherits?

(Joshua Burdick) #6

TcgObject just has functions for caching data. I’ll publish it with the overall but it won’t be necessary to get your application working and running effectively.

(Gus Landt) #7

Sure enough! I am able to connect and get my bearer token.

If I may be so bold as to make 1 more request. I am having difficulty formulating the code to start returning data. If you could show me the code to do anything, like returning the list of categories, I would be very grateful. I feel I’d be able to handle everything from there.

This is what I am trying to use, but it just gives a 401: Unauthorized
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(“application/json”));
client.DefaultRequestHeaders.Add(“Authorization”, {my bearer token});

var result = await client.GetAsync(“http://api.tcgplayer.com/v1.9.0/catalog/categories”).ConfigureAwait(false);

Edit: I think I’ve got it. I changed that 3rd line of code to:
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(“Bearer”, BearerToken);

And that seems to have done the trick! :smiley:

(Joshua Burdick) #8

Glad to hear its working for you now! Just let me know if you run into any other issues.

(Riley T. Prescott) #9

Thanks so much for the snippets. I was stuck on the same thing!

You said you would " I’ll publish it with the overall" Joshua.Burdock, Is this published already?

(Joshua Burdick) #10

It isn’t currently published and I don’t currently have a time frame for when it will be available.

(Riley T. Prescott) #11

No worries!
Really helpful what you already posted.
This is my first time trying to build a Client.

Thanks so much. :slight_smile:

(Josh Wilhelmi) #12

How’s the C# library coming along? I was tempted to build my own but didn’t want to reinvent the wheel if it’s done or almost done.