SurveyMonkey API - 未找到授权令牌

时间:2014-11-17 16:40:52

标签: c# surveymonkey

我在大约8个月前构建了一个C#应用程序来提取SurveyMonkey响应并将它们存储在我们的SQL Server上。该应用程序每天运行超过6个月没有任何问题,但突然星期五早上我无法得到任何要求通过。我在开发者网站上没有看到任何关于中断的内容,因此我一直在检查我的代码是否存在可能的问题。

这是我用来创建网络请求的第一个应用程序,所以我可能会做得很糟糕。

来自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"]
}

来自Fiddler的回应团体:

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

C#代码:

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 个答案:

答案 0 :(得分:0)

花了一段时间,但我设法与公司IT部门的某个人取得联系,他们监控流量,看看发生了什么。他们对代理服务器实施了修复,一切都终于再次运行了。