0

As the title states, I just need help with the first part of OAuth1.0 authentication: obtaining the request token. I am doing this in a console application using C#. I have been working on this for 3 days now and I have tried numerous samples from the internet, but so far nothing works. Here is my current attempt:

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


namespace MCAPIClient
{
    class Program
    {
        static void Main(string[] args)
        {
           RunAsync().Wait();
        }

        static async Task RunAsync()
        {
            using (var client = new HttpClient())
            {

                var values = new Dictionary<string, string>
                {
                   { "oauth_consumer_key", "<my consumer key>" },
                   { "oauth_consumer_secret", "<my secret key>" }
                };

                var content = new FormUrlEncodedContent(values);

                var response = await client.PostAsync("https://app.masteryconnect.com/oauth/request_token", content);

                var responseString = await response.Content.ReadAsStringAsync();

                Console.WriteLine(response.Headers);

                Console.WriteLine("Press any key to continue...");
                Console.ReadKey(true);
            }
        }

    }
}

This works beautifully when consuming an API without authentication (like http://rest-service.guides.spring.io/greeting), but receives 401 Forbidden when running it like this. What am I missing? BTW, I'm brand new to API's.

Daniel
  • 145
  • 1
  • 13

1 Answers1

-1

You must have a key and secret, or username and password. Both of them are very necessary to fetch access_token. i suggest use google postman tool it makes life lot easier in case of api's. Below code will solve your problem.

 WebRequest req = WebRequest.Create(url);
        req.Method = "POST";
        req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("key:Secret"));
        req.Credentials = new NetworkCredential("username", "password");


        var postData = "grant_type=client_credentials";

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = byteArray.Length;
        Stream dataStream = req.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();
        WebResponse response = req.GetResponse();
        // Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();

        Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(responseFromServer);

        String status = (string)o.SelectToken(".access_token");
saeedeh
  • 89
  • 1
  • 13
Gaurav Tyagi
  • 958
  • 6
  • 10