我正在使用TweetSharp向用户发送tweet(目前正在测试它),但是它总是返回错误的身份验证数据
{"errors":[{"code":215,"message":"Bad Authentication data."}]}
我已经检查了我的应用程序设置,它有完整的读写权限。我也尝试过重新生成我的消费者密钥,但仍然没有运气。
这是我的代码
public ActionResult AccessToken()
{
string oauth_consumer_key = "<consumer key>";
string oauth_consumer_secret = "<consumer secret>";
var service = new TwitterService(oauth_consumer_key, oauth_consumer_secret);
// Now we need the Token and TokenSecret
OAuthRequestToken requestToken = service.GetRequestToken("http://localhost:37808/");
string authURL = service.GetAuthorizationUri(requestToken).ToString();
Process.Start(authURL);
SendTweetOptions options = new SendTweetOptions();
options.Status = "Hello there Twitter";
service.SendTweet(options);
var re = service.Response.Response;
return View();
}
我做错了什么吗?
终于解决了问题,运行良好。根据约特的评论。
public ActionResult AccessToken()
{
// Step 1 - Retrieve an OAuth Request Token
TwitterService service = new TwitterService(ConfigurationManager.AppSettings["TwitterConsumerKey"], ConfigurationManager.AppSettings["TwitterConsumerSecret"]);
// This is the registered callback URL
OAuthRequestToken requestToken = service.GetRequestToken("http://localhost:37808/Twitter/OToken");
// Step 2 - Redirect to the OAuth Authorization URL
Uri uri = service.GetAuthorizationUri(requestToken);
return new RedirectResult(uri.ToString(), false /*permanent*/);
//return View();
}
public ActionResult OToken()
{
return View();
}
public ActionResult UserInfo(string oauth_token, string oauth_verifier)
{
var requestToken = new OAuthRequestToken { Token = oauth_token };
// Step 3 - Exchange the Request Token for an Access Token
TwitterService service = new TwitterService(ConfigurationManager.AppSettings["TwitterConsumerKey"],
ConfigurationManager.AppSettings["TwitterConsumerSecret"]);
OAuthAccessToken accessToken = service.GetAccessToken(requestToken, oauth_verifier);
// Step 4 - User authenticates using the Access Token
service.AuthenticateWith(accessToken.Token, accessToken.TokenSecret);
TwitterUser user = service.VerifyCredentials(new VerifyCredentialsOptions());
ViewBag.Message = string.Format("{0}", user.ScreenName);
// Step 5 - Send Tweet to User TimeLine
SendTweetOptions options = new SendTweetOptions();
string URL = "file:\C:\Users\<User>\Desktop\test.jpg";
string path = new Uri(URL).LocalPath;
// Sending with Media
using (var stream = new FileStream(path, FileMode.Open))
{
service.SendTweetWithMedia(new SendTweetWithMediaOptions
{
Status = "<status>",
Images = new Dictionary<string, Stream> { { path, stream } }
});
}
var responseText = service.Response.StatusCode;
if (responseText.ToString() == "OK")
{
ViewBag.Message = "Tweet Successful";
}
else
{
ViewBag.Message = "Tweet Unsuccessful";
}
return View();
}
}
我不相信你可以仅仅作为消费者发送Tweets, Tweets必须由用户帐户"拥有"。您需要注册一个Twitter帐户,然后执行完整的身份验证过程以获得访问令牌(除了消费者令牌之外),然后使用这两个令牌重新授权TweetSharp服务。
你上面的代码几乎达到了(我认为)。过程结束后。启动调用需要有逻辑来使用浏览器中返回的验证器(用户登录后显示的一个数字)来完成验证过程并充当该用户。目前,你的代码只完成了一半的过程,但还没有完成,所以当你尝试推特时,你的TweetSharp服务只被授权为应用程序,而不是用户。
原始的tweetsharp自述文件。Md确实包含了缺失的代码位。步骤3需要登录后浏览器返回的实际验证者:
//步骤3 -将请求令牌交换为访问令牌
string verifier = "123456";//<——这是你的用户在你的应用程序中输入的
OAuthAccessToken access = service。GetAccessToken (requestToken,匹配器);
//步骤4 -用户使用访问令牌进行身份验证service.AuthenticateWith(访问。令牌,access.TokenSecret);
//现在你的tweet调用应该在这里工作了。
它也看起来像你在服务器上的web应用程序做这个?在这种情况下,您使用的是完全错误的验证流(我相信)。这个是为桌面应用程序设计的,因此调用会启动一个新的浏览器进程供用户登录。我不完全确定web流程是如何工作的,因为我从未使用过它,但我相信你需要将用户重定向到你收到的授权url,并且在Twitter上注册的回调应该指向你的网站。我认为有某种状态参数可以通过oauth流传递回来,这样你就可以实现自己的逻辑,根据会话id或类似的东西来拾取你离开的地方。
我以前研究过这个课题。您必须在发送tweet之前注册开发者帐户,因为您需要令牌和密钥。这是我的windows服务项目。我在App.config
中编写了令牌和键代码 <appSettings>
<add key="twitterAccessToken" value="*****"/>
<add key="twitterAccessTokenSecret" value="*****"/>
<add key="twitterConsumerKey" value="*****"/>
<add key="twitterConsumerSecret" value="*****"/>
public static void SendTweet()
{
try
{
GetPixelImageFile();
string key = ConfigurationSettings.AppSettings.Get("twitterConsumerKey");
string secret = ConfigurationSettings.AppSettings.Get("twitterConsumerSecret");
string token = ConfigurationSettings.AppSettings.Get("twitterAccessToken");
string tokenSecret = ConfigurationSettings.AppSettings.Get("twitterAccessTokenSecret");
string message = "Color, Colorful, Pixel, Art, PixelColouring, Follow";
var service = new TweetSharp.TwitterService(key, secret);
service.AuthenticateWith(token, tokenSecret);
using (var stream = new FileStream(@"C:ImagesPixel.png", FileMode.Open))
{
var result = service.SendTweetWithMedia(new SendTweetWithMediaOptions
{
Status = message,
Images = new Dictionary<string, Stream> { { "john", stream } }
});
SendMail("SendTweet", (result == null ? "" : result.Text));
}
}
catch (Exception ex)
{
SendMail("SendTweet", ex.Message);
}
}