问题
I'm trying to develop a feature to post tweets on my Twitter account, in an MVC application.
For this, I have created an App in Twitter, and so have the following already: Consumer API Key, Consumer API Secret Key, Access token, Access token secret.
In the App details, the website URL is http://example.com and the callback URL field is empty.
The permission is set to - Read, Write and Direct messages.
Here is my code:
public static void Tweet(string message)
{
string twitterURL = "https://api.twitter.com/1.1/statuses/update.json";
string oauth_consumer_key = GlobalConstants.TWConsumerAPIKey;
string oauth_consumer_secret = GlobalConstants.TWConsumerAPISecretKey;
string oauth_token = GlobalConstants.TWAccessToken;
string oauth_token_secret = GlobalConstants.TWAccessTokenSecret;
// set the oauth version and signature method
string oauth_version = "1.0";
string oauth_signature_method = "HMAC-SHA1";
// create unique request details
string oauth_nonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
System.TimeSpan timeSpan = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc));
string oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();
// create oauth signature
string baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" + "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}";
string baseString = string.Format(
baseFormat,
oauth_consumer_key,
oauth_nonce,
oauth_signature_method,
oauth_timestamp, oauth_token,
oauth_version,
Uri.EscapeDataString(message)
);
string oauth_signature = null;
using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(Uri.EscapeDataString(oauth_consumer_secret) + "&" + Uri.EscapeDataString(oauth_token_secret))))
{
oauth_signature = Convert.ToBase64String(hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes("POST&" + Uri.EscapeDataString(twitterURL) + "&" + Uri.EscapeDataString(baseString))));
}
// create the request header
string authorizationFormat = "OAuth oauth_consumer_key=\"{0}\", oauth_nonce=\"{1}\", " + "oauth_signature=\"{2}\", oauth_signature_method=\"{3}\", " + "oauth_timestamp=\"{4}\", oauth_token=\"{5}\", " + "oauth_version=\"{6}\"";
string authorizationHeader = string.Format(
authorizationFormat,
Uri.EscapeDataString(oauth_consumer_key),
Uri.EscapeDataString(oauth_nonce),
Uri.EscapeDataString(oauth_signature),
Uri.EscapeDataString(oauth_signature_method),
Uri.EscapeDataString(oauth_timestamp),
Uri.EscapeDataString(oauth_token),
Uri.EscapeDataString(oauth_version)
);
HttpWebRequest objHttpWebRequest = (HttpWebRequest)WebRequest.Create(twitterURL);
objHttpWebRequest.Headers.Add("Authorization", authorizationHeader);
objHttpWebRequest.Method = "POST";
objHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
using (Stream objStream = objHttpWebRequest.GetRequestStream())
{
byte[] content = ASCIIEncoding.ASCII.GetBytes("status=" + Uri.EscapeDataString(message));
objStream.Write(content, 0, content.Length);
}
var responseResult = "";
try
{
//success posting
WebResponse objWebResponse = objHttpWebRequest.GetResponse();
StreamReader objStreamReader = new StreamReader(objWebResponse.GetResponseStream());
responseResult = objStreamReader.ReadToEnd().ToString();
}
catch (Exception ex)
{
responseResult = "Twitter Post Error: " + ex.Message.ToString() + ", authHeader: " + authorizationHeader;
}
}
In the catch block, there is this exception:
The remote server returned an error: (401) Unauthorized.
What am I doing wrong here.
Also, how do we know that the message is going to be posted to a particular account. Is it because I'm using the API keys from an APP created with my twitter credentials. But it allows me to create another app too.
回答1:
I copied your code and tested it, and it worked perfectly.
I didn't change anything except the keys to use my account (to answer your question yes all the applications are bound to your twitter account used to created your dev account). If you want your users to tweet on their personal account you'll have to ask them permission first : https://developer.twitter.com/en/docs/basics/authentication/overview/oauth.html
I managed to get your error in two cases :
- wrong keys : I changed the permission without regenerating the keys.
- wrong permissions : I changed the permission to read only and regenerated the keys.
回答2:
Try this.
var oauth_consumer_key = "gjxG99ZA5jmJoB3FeXWJZA";
var oauth_consumer_secret = "rsAAtEhVRrXUTNcwEecXqPyDHaOR4KjOuMkpb8g";
if (Request["oauth_token"] == null)
{
OAuthTokenResponse reqToken = OAuthUtility.GetRequestToken(
oauth_consumer_key,
oauth_consumer_secret,
Request.Url.AbsoluteUri);
Response.Redirect(string.Format("http://twitter.com/oauth/authorize?oauth_token={0}",
reqToken.Token));
}
else
{
string requestToken = Request["oauth_token"].ToString();
string pin = Request["oauth_verifier"].ToString();
var tokens = OAuthUtility.GetAccessToken(
oauth_consumer_key,
oauth_consumer_secret,
requestToken,
pin);
OAuthTokens accesstoken = new OAuthTokens()
{
AccessToken = tokens.Token,
AccessTokenSecret = tokens.TokenSecret,
ConsumerKey = oauth_consumer_key,
ConsumerSecret = oauth_consumer_secret
};
TwitterResponse<TwitterStatus> response = TwitterStatus.Update(
accesstoken,
"Testing!! It works (hopefully).");
if (response.Result == RequestResult.Success)
{
Response.Write("we did it!");
}
else
{
Response.Write("it's all bad.");
}
}
回答3:
I was having the same error.
Then I found that I was trying to post the message that I posted before. Twitter does not allow this to avoid spamming. I changed my message then it worked without issue.
Hope this helps someone.
来源:https://stackoverflow.com/questions/52585806/how-to-post-a-tweet-on-my-twitter-account-using-c-sharp