SurveyMonkey API - Authorization Token Not Found

866 Views Asked by At

I built a C# application about 8 months ago to pull SurveyMonkey responses and store them on our SQL Server. The application has run every day for over 6 months without any issue, but suddenly Friday morning I can't get any requests to go through. I'm not seeing anything on the developer site mentioning outages, so I've been examining my code for possible issues.

This is the first app I've ever written to create web requests, so it's possible I'm doing things badly.

Request, from Fiddler:

POST https://api.surveymonkey.net/v2/surveys/get_survey_list?api_key=<key hidden> HTTP/1.1
Authorization: bearer <token hidden>
Content-Type: application/json
Host: api.surveymonkey.net
Content-Length: 146
Expect: 100-continue
Connection: Keep-Alive

{
"page": 1,
"fields": ["title","analysis_url","preview_url","date_created","date_modified","language_id","question_count","num_responses"]
}

Response body, from Fiddler:

{"status":1,"errmsg":"Request header \"Authorization\" token not found"}

C# code:

private JObject SubmitPostRequest(string URIPath, string RequestBody)
{
    using (var webClient = new WebClient())
    {
        // Create URI for POST request to go to. URI varies depending on API method and includes API Key.
        Uri requestURI = new Uri(APIHost, URIPath + "?api_key=" + APIKey);

        // Have web client use NT credentials for proxy server
        webClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

        // Specify headers: access token in authorization header, content type
        webClient.Headers["Authorization"] = "bearer " + AccessToken;
        webClient.Headers["Content-Type"] = "application/json";

        // Create requestBody as byte[]
        byte[] requestBody = Encoding.Default.GetBytes(RequestBody);

        // Connect to the URI and send requestBody as a POST command.
        byte[] postResponse = webClient.UploadData(requestURI, "POST", requestBody);

        // Update LastConnect
        LastConnect = DateTime.Now;

        ++TransactionCounter;

        // Convert byte[] response to string, parse string and return as JObject.
        JObject jsonResponse = JObject.Parse(Encoding.Default.GetString(postResponse));

        // Evaluate "status" field of jsonResponse. Throw exception if response is not 0.
        dynamic dyn = jsonResponse;
        if (dyn.status != 0)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("HTTP Post request failed:");
            sb.Append("\tstatus: ").AppendLine(dyn.status.ToString());
            sb.Append("\terrmsg: ").AppendLine(dyn.errmsg.ToString());
            throw new WebException(sb.ToString());
        }

        return jsonResponse;
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

It took awhile, but I managed to get in touch with someone in corporate IT who monitored the traffic to see what was going on. They implemented a fix to the proxy server and everything is finally working again.