0

I am using the .NET HttpClient in xamarin forms to try to connect to survey monkey. I am new to HttpClient, Xamarin, REST, and survey monkey so I could be making mistakes anywhere or everywhere.

I've had some success using the survey monkey examples with curl and I am trying to convert that to C# code with HttpClient.

I have some "test code" like

HttpClient httpClient = new HttpClient();
Uri        uri        = new Uri("https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=humkanu389g5dp9tvsdrh8fv");
HttpContent content = new StringContent(String.Empty,Encoding.UTF8,"application/json");

httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization","Bearer XXXXXXXXX");
httpClient.Timeout = TimeSpan.FromSeconds(10);

HttpResponseMessage response = Task.Run(() => httpClient.PostAsync(uri,content)).Result;

String responseContent = Task.Run(() => response.Content.ReadAsStringAsync()).Result;

In my code, I have replaced the XXXXXXX's with my authorization token.

But I am getting a response like

{"status":1,"errmsg":"Invalid \"Authorization\" data in request header"}

I am wondering if I am handling the AuthenticationHeaderValue() part wrong. Or something else?

user2735420
  • 106
  • 5

2 Answers2

1

Your header is not in the correct format, you have "Authorization bearer XXXXX" as the value. So it looks like this:

Authorization: Authorization bearer XXXXX

I'm not sure why based on your code, you should debug and see the value you are sending out. But it should be:

Authorization: bearer XXXXX

My assumption is AuthenticationHeaderValue is doing something you're not expecting. Searching the docs it looks like you're doing it right - so I'm not sure exactly what's wrong with the C# code, just that the header you're sending out is not correct.

General Kandalaft
  • 2,215
  • 2
  • 18
  • 25
  • >> you should debug and see the value you are sending out. – user2735420 Oct 12 '16 at 16:37
  • >> you should debug and see the value you are sending out. – user2735420 Oct 12 '16 at 16:37
  • okay apparently I cannot put a line break in a comment. Its posting the comment when I try. What I was trying to say is "how?" I don't know how to "see" what I am sending. I haven't done REST and/or http stuff before. thanks – user2735420 Oct 12 '16 at 16:39
  • I think you are on the right track. I found a similar case in http://stackoverflow.com/questions/14627399/setting-authorization-header-of-httpclient and I tried the syntax httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer","XXXXXXX"); I am now getting a different error, I think unrelated to authorization. – user2735420 Oct 12 '16 at 16:50
0

General Kandalaft really pointed out the problem--which in retrospect was obvious, but I want to post the final code here in case someone else is trying this.

I ended up fixing the AuthenticationHeaderValue as pointed out above. Then I ran into a problem because my request content was an empty string. I put in some json text from an example and it worked. So the final code

HttpClient httpClient = new HttpClient();
Uri        uri        = new Uri("https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=humkanu389g5dp9tvsdrh8fv");
HttpContent content = new StringContent ("{ \"fields\": [ \"title\", \"date_modified\" ] }",Encoding.UTF8,"application/json");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer","XXXXXX");
httpClient.Timeout = TimeSpan.FromSeconds(10);

HttpResponseMessage response = Task.Run(() => httpClient.PostAsync(uri,content)).Result;

String responseContent = Task.Run(() => response.Content.ReadAsStringAsync()).Result;
user2735420
  • 106
  • 5