Jump to content
  • 0

C# Get Token


Pingzinggolf

Question

I am new to coding to REST API and could use some help.

I am trying to get my token in a C# application.  I think I am close but I am getting back - Bad request

 

Here is my code.  Any help would be appreciated.

           var baseUri = new Uri("https://c2afw1111111111111.caspio.com/");
            var encodedConsumerKey = HttpUtility.UrlEncode("111111111111111111111111111");
            var encodedConsumerKeySecret = HttpUtility.UrlEncode("111111111111111111111111111");
            var encodedPair = Base64Encode(String.Format("{0}:{1}", encodedConsumerKey, encodedConsumerKeySecret));

            var requestToken = new HttpRequestMessage
            {
                Method = HttpMethod.Post,
                RequestUri = new Uri(baseUri, "oauth/token"),
                Content = new StringContent("grant_type=client_credentials")
            };

            requestToken.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded") { CharSet = "UTF-8" };
            requestToken.Headers.TryAddWithoutValidation("Authorization", String.Format("Basic {0}", encodedPair));


            using (HttpClient client = new HttpClient())
            {
                using (HttpResponseMessage response = client.SendAsync(requestToken).Result)
                {
                    using (HttpContent content = response.Content)
                    {
                        var json = content.ReadAsStringAsync().Result;
                    }
                }
            }

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

@Pingzinggolf

this worked for me
 

            var baseUri = new Uri("https://c2afw1111111111111.caspio.com/");

            var requestToken = new HttpRequestMessage

            {

                Method = HttpMethod.Post,

                RequestUri = new Uri(baseUri, "oauth/token"),

                Content = new StringContent("grant_type=client_credentials&client_id=1111222333111222 &client_secret=4445556667877888")

            };

            using (HttpClient client = new HttpClient())

            {

                using (HttpResponseMessage response = client.SendAsync(requestToken).Result)

                {

                    using (HttpContent content = response.Content)

                    {

                        var json = content.ReadAsStringAsync().Result;

                        Console.WriteLine(json);

                        Console.ReadLine();

                    }

                }

            }

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...