Reddit.NET OAuth认证失败:MissingMethodException错误求助
问题
使用Reddit.NET库实现OAuth认证时触发System.MissingMethodException,提示找不到RestSharp.IRestRequest的AddHeader方法。已确认重定向地址http://localhost:8080/Reddit.NET/oauthRedirect配置正确,AppID与AppSecret无误。
相关代码:
public class RedditManager { private readonly RedditClient _redditClient; public RedditManager(string appID, string appSecret) { string refreshToken = AuthorizeUser(appID, appSecret); _redditClient = new RedditClient(appID, refreshToken, appSecret); } public static string AuthorizeUser(string appId, string appSecret = null, int port = 8080) { // Create a new instance of the auth token retrieval library. --Kris AuthTokenRetrieverLib authTokenRetrieverLib = new AuthTokenRetrieverLib(appId, appSecret, port); // Start the callback listener. --Kris // Note - Ignore the logging exception message if you see it. You can use Console.Clear() after this call to get rid of it if you're running a console app. authTokenRetrieverLib.AwaitCallback(); // Open the browser to the Reddit authentication page. Once the user clicks "accept", Reddit will redirect the browser to localhost:8080, where AwaitCallback will take over. --Kris OpenBrowser(authTokenRetrieverLib.AuthURL()); // Replace this with whatever you want the app to do while it waits for the user to load the auth page and click Accept. --Kris while (true) { } // Cleanup. --Kris authTokenRetrieverLib.StopListening(); return authTokenRetrieverLib.RefreshToken; } private static void OpenBrowser(string authUrl, string browserPath = @"C:\Program Files\BraveSoftware\Brave-Browser\Application\brave.exe") { try { ProcessStartInfo processStartInfo = new ProcessStartInfo(authUrl); Process.Start(processStartInfo); } catch (System.ComponentModel.Win32Exception) { // This typically occurs if the runtime doesn't know where your browser is. Use BrowserPath for when this happens. --Kris ProcessStartInfo processStartInfo = new ProcessStartInfo(browserPath) { Arguments = authUrl }; Process.Start(processStartInfo); } } public Task HotPosts(string subreddit) { var posts = _redditClient.Subreddit(subreddit).Posts.Hot; List<Post> videoposts = new List<Post>(); foreach (var post in posts) { if (post.Permalink.Contains(".mp4")) { videoposts.Add(post); } } foreach (var post in videoposts) { Console.WriteLine(post.Info()); } return Task.CompletedTask; } }
控制台错误日志:
2024-01-24 11:40:27 [INFO] (uhttpsharp.HttpServer) Embedded uhttpserver started. 2024-01-24 11:40:37 [INFO] (uhttpsharp.HttpClientHandler) Got Client 127.0.0.1:59132 2024-01-24 11:40:37 [INFO] (uhttpsharp.HttpServer) Embedded uhttpserver stopped. 2024-01-24 11:40:37 [INFO] (uhttpsharp.HttpClientHandler) 127.0.0.1:59132 : Got request /Reddit.NET/oauthRedirect 2024-01-24 11:40:37 [WARN] (uhttpsharp.HttpClientHandler) Error while serving : 127.0.0.1:59132 System.MissingMethodException Method not found: 'RestSharp.IRestRequest RestSharp.RestRequest.AddHeader(System.String, System.String)'. at Reddit.AuthTokenRetriever.AuthTokenRetrieverLib.<>c__DisplayClass37_0.<AwaitCallback>b__0(IHttpContext context, Func`1 next) at uhttpsharp.AnonymousHttpRequestHandler.Handle(IHttpContext context, Func`1 next) at uhttpsharp.RequestHandlersAggregateExtensions.<>c__DisplayClass1_0.<Aggregate>b__0(IHttpContext context) at uhttpsharp.HttpClientHandler.<Process>d__11.MoveNext() 2024-01-24 11:40:37 [INFO] (uhttpsharp.HttpClientHandler) Lost Client 127.0.0.1:59132
解决方案
错误原因
该异常是RestSharp版本不兼容导致的:Reddit.NET依赖的RestSharp版本中,AddHeader方法的返回值类型或签名与你项目中当前引用的RestSharp版本不匹配。例如旧版RestSharp的AddHeader返回void,而Reddit.NET预期的版本中该方法返回IRestRequest,运行时因找不到对应签名的方法而抛出异常。
解决步骤
- 统一RestSharp版本:卸载当前项目中的RestSharp包,重新安装与Reddit.NET兼容的版本。在NuGet包管理器控制台执行:
可通过查看Reddit.NET的NuGet依赖信息,获取它要求的RestSharp版本范围,选择该范围内的版本安装。Uninstall-Package RestSharp Install-Package RestSharp -Version [Reddit.NET依赖的版本号] - 清理重建项目:执行解决方案的
Clean Solution操作,再执行Rebuild Solution,确保所有依赖项正确加载并编译。 - 排查依赖冲突:如果项目中其他第三方库也引用了RestSharp,需通过NuGet依赖管理功能统一所有库使用的RestSharp版本,避免版本不一致引发的冲突。
内容的提问来源于stack exchange,提问作者VindE
相关产品推荐
相关产品推荐

