0

i am using following code for text post in reddit.com but it gives bad captcha error,how can we get captcha and pass it in code. i am stuck there please help..

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

i am using this link "https://www.reddit.com/dev/api/" for reference.plzz help

Sanjiv Rajput
  • 145
  • 1
  • 13

1 Answers1

0

I looked at the GitHub page for RedditSharp and it doesn't seem like there is a way to solve the captcha with code from the API (which is what you are asking for correct?) To do that you're going to have to look into solving captcha with code, which is too advanced for me. You can look into something like 2Captcha to solve them if you would rather not have users do it.

if not....

In the RedditSharp api it has a built in 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;
        }
    }
}

That will return a link to a captcha that you can view in a web browser, you just enter what you believe the captcha to be and return the captcha response with the request (as stated in the API).

The line

var post = sub.SubmitTextPost("RedditSharp test", "This is a test post sent from RedditSharp");

Is where I believe you are getting this error. SubmitTextPost just calls the method Submit and that returns the Post object. But to submit a new post you need to fill out a captcha (which is required), but the Submit method itself does call HandleCaptcha method in ConsoleCaptchaSolver so you should be seeing the captcha ID and URL to be able to see and provide a response too. If not I recommend using NuGet to make sure you have the most up-to-date RedditSharper package.

EDIT:

I looked at the RedditSharp TestRedditSharp project and commented out a part that wasn't important, you can see it here https://gist.github.com/brian-heidrich/af61bef6a60438b2cfbf80e557d2ac5f

I was able to use that code and make a test post to /r/DTL https://www.reddit.com/r/DTL/comments/4yuu4f/redditsharp_test/

briandoesdev
  • 141
  • 6