gpt4 book ai didi

C# TweetSharp 不发送推文

转载 作者:行者123 更新时间:2023-11-30 20:34:24 25 4
gpt4 key购买 nike

我正在使用 TweetSharp 向用​​户发送推文(目前正在测试),但它不断返回错误的身份验证数据

{"errors":[{"code":215,"message":"Bad Authentication data."}]}

我已经检查了我的应用程序设置,它具有完全的读写权限。我也尝试过重新生成我的消费者 key ,但还是不行。

这是我的代码

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();
}

我做错了什么吗?

最佳答案

终于解决了这个问题,效果很好。基于 Yort 的评论。

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();
}
}

关于C# TweetSharp 不发送推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39297147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com