2

我创建了一个小包装器来访问 Linked In API,我传递给它的参数如下:

[oauth_verifier, 88600]
[oauth_token, xxxx]
[oauth_version, 1.0]
[oauth_nonce, xxx]
[oauth_timestamp, xxxxx]
[oauth_signature_method, HMAC-SHA1]
[oauth_consumer_key, xxxxxx]
[oauth_consumer_secret, xxxxxx]
[oauth_signature, xxx+xxxxxxxxxxx]

我得到的异常如下:“远程服务器返回错误:(401)未经授权。”

我正在尝试通过非常适合 twitter 的 Web 请求访问它,您可以查看以下代码:

public static OAuthTokenResponse GetAccessToken(string consumerKey, string consumerSecret, string requestToken, string verifier, string getAccessTokenUrl, string realM)
    {
        if (string.IsNullOrEmpty(consumerKey))
        {
            throw new ArgumentNullException("consumerKey");
        }

        if (string.IsNullOrEmpty(consumerSecret))
        {
            throw new ArgumentNullException("consumerSecret");
        }

        if (string.IsNullOrEmpty(requestToken))
        {
            throw new ArgumentNullException("requestToken");
        }



        WebRequestBuilder builder = new WebRequestBuilder(
            new Uri(getAccessTokenUrl),
            HTTPVerb.GET,
            new OAuthTokens { ConsumerKey = consumerKey, ConsumerSecret = consumerSecret });
        builder.Realm = realM;

        if (!string.IsNullOrEmpty(verifier))
        {
            builder.Parameters.Add("oauth_verifier", verifier);
        }


        builder.Parameters.Add("oauth_token", requestToken);

        string responseBody;

        try
        {
            HttpWebResponse webResponse = builder.ExecuteRequest();

            responseBody = new StreamReader(webResponse.GetResponseStream()).ReadToEnd();
        }
        catch (WebException wex)
        {
            throw new Exception(wex.Message, wex);
        }

        OAuthTokenResponse response = new OAuthTokenResponse();
        response.Token = Regex.Match(responseBody, @"oauth_token=([^&]+)").Groups[1].Value;
        response.TokenSecret = Regex.Match(responseBody, @"oauth_token_secret=([^&]+)").Groups[1].Value;
        response.UserId = long.Parse(Regex.Match(responseBody, @"user_id=([^&]+)").Groups[1].Value, CultureInfo.CurrentCulture);
        response.ScreenName = Regex.Match(responseBody, @"screen_name=([^&]+)").Groups[1].Value;
        return response;
    }

当我点击执行请求时出现错误。

Web 请求生成器执行以下操作:

public HttpWebResponse ExecuteRequest()
{
        HttpWebRequest request = PrepareRequest();
  return (HttpWebResponse)request.GetResponse();
}

我想我在请求中缺少一个参数,有什么想法吗?

4

0 回答 0