OAuth RequestToken返回403

时间:2014-05-06 17:39:47

标签: c# oauth webrequest

我尝试使用简单的类实现OAuth,但我收到错误

public OAuthResponse AcquireRequestToken(string uri, string method) 
{ 
    NewRequest(); 
    var authzHeader = GetAuthorizationHeader(uri, method); 

    // prepare the token request 
    var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri); 
    request.Headers.Add("User-Agent", "MyAppId/1.0.3 +http://example.pt/"); 
    request.Headers.Add("Authorization", authzHeader); 
    request.Method = method; 
    request.ContentLength = 0; 

    using (var response = (System.Net.HttpWebResponse)request.GetResponse()) 
    { 
        using (var reader = new System.IO.StreamReader(response.GetResponseStream())) 
        { 
            var r = new OAuthResponse(reader.ReadToEnd()); 
            this["token"] = r["oauth_token"]; 

            // Sometimes the request_token URL gives us an access token, 
            // with no user interaction required. Eg, when prior approval 
            // has already been granted. 
            try 
            { 
                if (r["oauth_token_secret"] != null) 
                this["token_secret"] = r["oauth_token_secret"]; 
            } 
            catch { } 
            return r; 
        } 
    } 
} 

此行中的错误

var response = (System.Net.HttpWebResponse)request.GetResponse()

,错误是

  

其他信息:远程服务器返回错误:(403)   禁止的。

任何帮助?以下是完整的OAuth类https://cropperplugins.codeplex.com/SourceControl/changeset/view/65377#1710422

我尝试访问它的服务也是Discogs.com API DOCS:http://www.discogs.com/developers

更新1

我已添加了用户代理标头,discogs工作人员表示需要它,但现在我在User-Agent行request.Headers.Add("User-Agent", "BandFollowerWD/1.0.3 +http://pcdev.pt/");

上又出现了一个错误
The 'User-Agent' header must be modified with the appropriate property or method.

1 个答案:

答案 0 :(得分:0)

经过仔细搜索后,我找到了答案.... 我没有使用request.Headers.Add("User-Agent", "MyAppId/1.0.3 +http://example.pt");,而是使用了request.UserAgent = "MyAppId/1.0.3 +http://example.pt";