0

I have this code here in python

response = requests.post(
    "https://gateway.watsonplatform.net/personality-insights/api/v2/profile",
    auth = ("username", "password"),
    headers = {"content-type": "text/plain"},
    data = "your text goes here"
)

jsonProfile = json.loads(response.text)

I am trying to convert it to C#, below is my code:

public void getRequest() {
        string url = "https://gateway.watsonplatform.net/personality-insights/api/v2/profile";
        using (var client = new WebClient())
        {
            var values = new NameValueCollection();
            values["username"] = username;
            values["password"] = password;
            values["content-type"] = "text/plain";
            values["data"] = getTestWords(@"D:\Userfiles\tchaiyaphan\Documents\API and Intelligence\storyTestWord.txt");

            var response = client.UploadValues(url, values);

            var responseString = Encoding.Default.GetString(response);
        }
    }

I don't know what to do with header section so i left that out. And when i ran the code it gave me an 401 error. I dont know what to do!

4
  • Debug and check the values of username and password Commented Aug 26, 2015 at 7:38
  • http error 401 means "Unauthorized". Make sure that the username and password are incorrect. Commented Aug 26, 2015 at 7:39
  • If the C# fails with 401, then python will as well. This has little to do with converting code between languages Commented Aug 26, 2015 at 7:40
  • For the headers try WebClient.Headers! Commented Aug 26, 2015 at 7:42

2 Answers 2

1

The problem is that your code is sending username and password as POST data instead of using the proper HTTP authorization header.

client.Credentials = new NetworkCredential(username, password);
Sign up to request clarification or add additional context in comments.

1 Comment

WOW thanks. Now The remote server returned an error: (415) Unsupported Media Type. It stops at line: var response = client.UploadValues(url, values);
0

Although ThiefMaster had manage to get me pass the authentication but it gave me a different error this time (415 Un-supported media type) so i decided to take a different approach and it works.

var request = (HttpWebRequest)WebRequest.Create("https://gateway.watsonplatform.net/personality-insights/api/v2/profile");

        var postData = getTestWords(@"D:\Userfiles\tchaiyaphan\Documents\API and Intelligence\storyTestWord.txt");

        var data = Encoding.ASCII.GetBytes(postData);

        request.Method = "POST";
        request.ContentType = "text/plain";
        request.ContentLength = data.Length;
        request.Credentials = new NetworkCredential(username, password);

        using (var stream = request.GetRequestStream())
        {
            stream.Write(data, 0, data.Length);
        }

        var response = (HttpWebResponse)request.GetResponse();

        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.