使用InstaSharper上传图片报错:解析值时遇意外字符
解决InstaSharper上传图片返回404 HTML的问题
问题分析
你遇到的Unexpected character encountered while parsing value: <错误,本质是上传请求返回了404的HTML页面(而非预期的JSON),且HTML里标记了not-logged-in——这说明上传请求时没有携带有效的登录会话,虽然LoginAsync返回成功,但实际会话可能未正确初始化或被后续请求丢失。
解决方案
1. 先验证登录会话有效性
登录后主动获取当前用户信息,确认会话真实有效:
if (loginResult.Succeeded) { Console.WriteLine("Logged in successfully."); // 新增:验证会话有效性 var userInfo = await _instaApi.GetCurrentUserAsync(); if (!userInfo.Succeeded) { Console.WriteLine($"会话无效:{userInfo.Info.Message}"); return; } Console.WriteLine($"当前登录用户:{userInfo.Value.Username}"); await InsertImage(@"C:\Insta\testImage.png"); }
2. 修正图片路径传递方式
你当前通过Uri.LocalPath转换路径的方式可能导致格式异常,直接使用完整文件路径即可:
public static async Task InsertImage(string _path) { var fullPath = Path.GetFullPath(_path); Console.WriteLine(fullPath); var mediaImage = new InstaImage { URI = fullPath, // 直接用完整路径,无需Uri转换 Width = 1080, Height = 1080 }; Console.WriteLine("Media Made"); var result = await _instaApi.UploadPhotoAsync(mediaImage, "Caption"); if (result.Succeeded) Console.WriteLine($"Media created: {result.Value.Pk}, {result.Value.Caption}"); else Console.WriteLine($"Unable to upload photo: {result.Info.Message}"); Console.WriteLine("End of Function"); }
3. 改用文件流上传(更可靠)
如果路径方式仍有问题,直接传入文件流避免路径解析错误:
public static async Task InsertImage(string _path) { using var imageStream = new FileStream(_path, FileMode.Open, FileAccess.Read); var mediaImage = new InstaImage { InputStream = imageStream, Width = 1080, Height = 1080 }; var result = await _instaApi.UploadPhotoAsync(mediaImage, "Caption"); // 后续输出逻辑不变 }
4. 升级InstaSharper到最新版本
Instagram的API端点经常更新,旧版本的InstaSharper可能指向已废弃的上传地址,导致404。请确保使用最新版本的NuGet包。
完整修改后的代码
private static IInstaApi _instaApi; private static async Task Main(string[] args) { var userCredentials = new UserSessionData { UserName = "MyInstagramUsername", Password = "MyInstagramPassword" }; _instaApi = InstaApiBuilder.CreateBuilder() .SetUser(userCredentials) .UseLogger(new DebugLogger(LogLevel.All)) .Build(); var loginResult = await _instaApi.LoginAsync(); if (loginResult.Succeeded) { Console.WriteLine("Logged in successfully."); // 验证会话有效性 var userInfo = await _instaApi.GetCurrentUserAsync(); if (!userInfo.Succeeded) { Console.WriteLine($"会话验证失败:{userInfo.Info.Message}"); return; } Console.WriteLine($"当前登录用户:{userInfo.Value.Username}"); await InsertImage(@"C:\Insta\testImage.png"); } else { Console.WriteLine($"Failed to log in: {loginResult.Info.Message}"); } } public static async Task InsertImage(string _path) { var fullPath = Path.GetFullPath(_path); Console.WriteLine(fullPath); var mediaImage = new InstaImage { URI = fullPath, Width = 1080, Height = 1080 }; Console.WriteLine("Media Made"); var result = await _instaApi.UploadPhotoAsync(mediaImage, "Caption"); if (result.Succeeded) Console.WriteLine($"Media created: {result.Value.Pk}, {result.Value.Caption}"); else Console.WriteLine($"Unable to upload photo: {result.Info.Message}"); Console.WriteLine("End of Function"); }
内容的提问来源于stack exchange,提问作者Chasper
相关产品推荐
相关产品推荐

