我在reddit.com中使用以下代码进行文本发布,但它提供了错误的验证码错误,我们如何获取验证码并将其传递给代码。我被困在那里请帮助..
class Program
{
static void Main(string[] args)
{
Reddit reddit = null;
var authenticated = false;
while (!authenticated)
{
Console.Write("OAuth? (y/n) [n]: ");
var oaChoice = Console.ReadLine();
if (!string.IsNullOrEmpty(oaChoice) && oaChoice.ToLower()[0] == 'y')
{
Console.Write("OAuth token: ");
var token = "neud__ZOgyo52UsffzF0Z2WJnN0";
reddit = new Reddit("neud__ZOgyo52UsffzF0Z2WJnN0");//HsmHqXmvK3xciDt_FkPQJ-Klq-M
reddit.InitOrUpdateUser();
authenticated = reddit.User != null;
if (!authenticated)
Console.WriteLine("Invalid token");
}
else
{
Console.Write("Username: ");
var username = Console.ReadLine();
Console.Write("Password: ");
var password = ReadPassword();
try
{
Console.WriteLine("Logging in...");
reddit = new Reddit(username, password);
authenticated = reddit.User != null;
}
catch (AuthenticationException)
{
Console.WriteLine("Incorrect login.");
authenticated = false;
}
}
}
Console.Write("Create post? (y/n) [n]: ");
var choice = Console.ReadLine();
if (!string.IsNullOrEmpty(choice) && choice.ToLower()[0] == 'y')
{
Console.Write("Type a subreddit name: ");
var subname = Console.ReadLine();
var sub = reddit.GetSubreddit("/r/subreddit");
Console.WriteLine("Making test post");
var post = sub.SubmitTextPost("RedditSharp test", "This is a test post sent from RedditSharp");
Console.WriteLine("Submitted: {0}", post.Url);
}
else
{
Console.Write("Type a subreddit name: ");
var subname = Console.ReadLine();
var sub = reddit.GetSubreddit("/r/subreddit");
foreach (var post in sub.GetTop(FromTime.Week).Take(10))
Console.WriteLine("\"{0}\" by {1}", post.Title, post.Author);
}
Console.ReadKey(true);
}
public static string ReadPassword()
{
var passbits = new Stack<string>();
//keep reading
for (ConsoleKeyInfo cki = Console.ReadKey(true); cki.Key != ConsoleKey.Enter; cki = Console.ReadKey(true))
{
if (cki.Key == ConsoleKey.Backspace)
{
if (passbits.Count() > 0)
{
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
Console.Write(" ");
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
passbits.Pop();
}
}
else
{
Console.Write("*");
passbits.Push(cki.KeyChar.ToString());
}
}
string[] pass = passbits.ToArray();
Array.Reverse(pass);
Console.Write(Environment.NewLine);
return string.Join(string.Empty, pass);
}
}
我正在使用此链接“https://www.reddit.com/dev/api/”作为reference.plzz帮助
答案 0 :(得分:0)
我查看了RedditSharp的GitHub页面,看起来似乎没有办法用API中的代码解决验证码(这是你要求的正确吗?)为此你做了#39 ;我将不得不考虑用代码解决验证码,这对我来说太先进了。如果您不想让用户这样做,您可以查看像2Captcha这样的东西来解决它们。
如果不是......
在RedditSharp api中,它有一个内置的ConsoleCaptchaSolver
using System;
namespace RedditSharp
{
public class ConsoleCaptchaSolver : ICaptchaSolver
{
public CaptchaResponse HandleCaptcha(Captcha captcha)
{
Console.WriteLine("Captcha required! The captcha ID is {0}", captcha.Id);
Console.WriteLine("You can find the captcha image at this url: {0}", captcha.Url);
Console.WriteLine("Please input your captcha response or empty string to cancel:");
var response = Console.ReadLine();
CaptchaResponse captchaResponse = new CaptchaResponse(string.IsNullOrEmpty(response) ? null : response);
return captchaResponse;
}
}
}
这将返回您可以在Web浏览器中查看的验证码的链接,您只需输入您认为的验证码,并返回带有请求的验证码响应(如API中所述)。
该行
var post = sub.SubmitTextPost("RedditSharp test", "This is a test post sent from RedditSharp");
我认为你在哪里得到这个错误。 SubmitTextPost只调用方法Submit并返回Post对象。 但要提交新帖子,您需要填写验证码(这是必需的),但Submit方法本身确实在ConsoleCaptchaSolver中调用了HandleCaptcha方法,因此您应该看到验证码ID和URL能够看到并提供回应。如果不是,我建议使用NuGet以确保您拥有最新的RedditSharper软件包。
编辑:
我查看了RedditSharp TestRedditSharp项目并注释掉了一个不重要的部分,你可以在这里看到它https://gist.github.com/brian-heidrich/af61bef6a60438b2cfbf80e557d2ac5f
我能够使用该代码并将测试发布到/ r / DTL https://www.reddit.com/r/DTL/comments/4yuu4f/redditsharp_test/