SurveyMonkey返回意外的EOF错误,交换访问令牌的授权代码

时间:2015-06-03 20:17:39

标签: oauth oauth-2.0 access-token surveymonkey system.net.httpwebrequest

在“localhost”环境中工作的代码在(显然)新的回调URL下部署到生产后失败了。没有其他改变。对SurveyMonkey的API交换授权代码以获取长期访问令牌的请求正在返回此错误:

  

System.Net.WebException:底层连接已关闭:发送时发生意外错误。 ---> System.IO.IOException:从传输流中收到意外的EOF或0字节。

是否有其他需要更改的内容,可能是生产client_id?请指教,我们被困住了。

以下是代码:

        var clientId = _sds.ByName("SurveyMonkeyClientID").Value;
        var apiSecret = _sds.ByName("SurveyMonkeyAPISecret").Value;
        var callbackURL = _sds.ByName("SurveyMonkeyCallbackURL").Value;
        var accessTokenUri = "https://api.surveymonkey.net/oauth/token?api_key=" + _sds.ByName("SurveyMonkeyAPIKey").Value;

        // Create postback data
        var postData = new NameValueCollection()
        {
            { "client_secret", apiSecret },
            { "code", authorizationCode },
            { "redirect_uri", callbackURL },
            { "client_id", clientId },
            { "grant_type", "authorization_code" }
        };

        var queryString = string.Join("&", (
            from key in postData.AllKeys
            from value in postData.GetValues(key)
            select string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value))).ToArray()
        );

        // Create and configure the web request object
        var request = WebRequest.Create(accessTokenUri);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        var dataStream = request.GetRequestStream();

        var postArray = Encoding.UTF8.GetBytes(queryString);
        dataStream.Write(postArray, 0, postArray.Length);
        dataStream.Close();

        // Make the call to Survey Monkey's token exchange uri
        try
        {
            var response = request.GetResponse();
            dataStream = response.GetResponseStream();
            var reader = new StreamReader(dataStream);
            var responseFromServer = reader.ReadToEnd();
            reader.Close();
            dataStream.Close();
            response.Close();

            SaveSurveyMonkeyJsonWhole(responseFromServer);
            dynamic jobject = _serializer.DeserializeObject(responseFromServer);
            var access_token = jobject["access_token"];
            return access_token;
        }
        catch (Exception ex)
        {
            ErrorSignal.FromCurrentContext().Raise(ex);
            return string.Empty;
        }

0 个答案:

没有答案