0

I'm at the first stage of trying to communicate with Twitter using DotNetOpenAuth. Whenever I call this method I receive the following error:

ProtocolException occured The remote server returned an error: (403) Forbidden.

public string SendConsumerAuthentication()
{
    ServiceProviderDescription serviceProvider = GetServiceDescription();

    string applicationKey = settingsManager.GetApplicationKey(className);
    string applicationSecret = settingsManager.GetApplicationSecret(className);

    // decouple this in future
    InMemoryTokenManager inMemoryTokenManager = new InMemoryTokenManager(applicationKey, applicationSecret);

    var consumer = new DesktopConsumer(serviceProvider, inMemoryTokenManager);
    string uri = string.Empty;
    string requestToken = string.Empty;

    var requestArgs = new Dictionary<string, string> { 
        //need to pass this as extra, but leave the value blank
        { "oauth_token", string.Empty} 
    };

    //request access
    try
    {                
        uri = consumer.RequestUserAuthorization(requestArgs, null, out requestToken).AbsoluteUri;
    }
    catch (Exception)
    {
        uri = null;                
    }

    return uri;
}

I wonder what i'm missing?

4

1 回答 1

0

改变我的端点解决了这个问题,

从:

    public override ServiceProviderDescription GetServiceDescription()
    {
        return new ServiceProviderDescription
        {
            AccessTokenEndpoint = new MessageReceivingEndpoint("https://api.twitter.com/oauth/access_token", HttpDeliveryMethods.PostRequest),
            RequestTokenEndpoint = new MessageReceivingEndpoint("https://api.twitter.com/oauth/authorize", HttpDeliveryMethods.PostRequest),
            UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://api.twitter.com/oauth/access_token", HttpDeliveryMethods.PostRequest),
            TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },
            ProtocolVersion = ProtocolVersion.V10
        };
    }

至:

    public override ServiceProviderDescription GetServiceDescription()
    {
        return new ServiceProviderDescription
        {
            AccessTokenEndpoint = new MessageReceivingEndpoint("https://api.twitter.com/oauth/access_token", HttpDeliveryMethods.PostRequest),
            RequestTokenEndpoint = new MessageReceivingEndpoint("https://api.twitter.com/oauth/request_token", HttpDeliveryMethods.PostRequest),
            UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://api.twitter.com/oauth/authorize", HttpDeliveryMethods.PostRequest),
            TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },
            ProtocolVersion = ProtocolVersion.V10
        };
    }
于 2013-05-28T10:02:49.850 回答