C# Instasharp在控制台应用程序中的应用
我有一个.net控制台应用程序,我想使用它通过Instasharp包装器使用标签搜索来获取Instagram帖子 我广泛使用C#.net web表单,不太熟悉MVC,也不太熟悉如何使用wait关键字。下面的代码示例似乎正在运行,但从未提供任何输出 这一行:C# Instasharp在控制台应用程序中的应用,c#,.net,console-application,instagram,instasharp,C#,.net,Console Application,Instagram,Instasharp,我有一个.net控制台应用程序,我想使用它通过Instasharp包装器使用标签搜索来获取Instagram帖子 我广泛使用C#.net web表单,不太熟悉MVC,也不太熟悉如何使用wait关键字。下面的代码示例似乎正在运行,但从未提供任何输出 这一行: var tagInfo = await tagApi.Get("soccer"); 将我返回到调用方法,而不指示检索到的数据 有人能提供我在这里做错了什么的见解吗 public static async void GetInstagram(
var tagInfo = await tagApi.Get("soccer");
将我返回到调用方法,而不指示检索到的数据
有人能提供我在这里做错了什么的见解吗
public static async void GetInstagram(String tag, InstagramConfig config)
{
var instagramPosts = await LoadInstagramPosts(tag, config);
dynamic dyn = JsonConvert.DeserializeObject(instagramPosts.ToString());
foreach (var data in dyn.data)
{
Console.WriteLine("{0} - {1}", data.filter, data.images.standard_resolution.url);
}
}
public static async Task<TagResponse> LoadInstagramPosts(String hashTagTerm, InstagramConfig config)
{
var tagApi = new InstaSharp.Endpoints.Tags(config);
var tagInfo = await tagApi.Get("soccer");
}
public static async void GetInstagram(字符串标记,InstagramConfig config)
{
var instagramPosts=等待加载instagramPosts(标记,配置);
dynamic dyn=JsonConvert.DeserializeObject(instagramPosts.ToString());
foreach(动态数据中的var数据)
{
Console.WriteLine(“{0}-{1}”,data.filter,data.images.standard_resolution.url);
}
}
公共静态异步任务LoadInstagramPosts(字符串hashTagTerm,InstagramConfig配置)
{
var tagApi=new InstaSharp.Endpoints.Tags(配置);
var tagInfo=wait tagApi.Get(“足球”);
}
在第一条注释后编辑代码,解决了我最初的问题
我觉得我离得很近,但仍然缺少一些东西。
请参阅下面的具体问题
我的代码基于InstaSharp GitHub()的文档。GitHubs示例基于一个MVC应用程序,mine不是一个MVC项目,而是一个控制台应用程序
我觉得我很接近,也许其他人会从帮助我解决这个问题中受益
我的具体问题。。。
1) 不确定OAuth方法中的“code”参数源自何处??
2) 如何使用Instagram执行所需的回拨
var config = new InstaSharp.InstagramConfig(location.InstagramClientId, location.InstagramClientSecret, "http://localhost");
string instagramLoginLink = InstagramLogin(config);
GetInstagram("soccer", config, instagramLoginLink);
public static async void GetInstagram(String tag, InstagramConfig config, string code)
{
OAuthResponse oAuthResponse = await OAuth(code, config);
var instagramPosts = await LoadInstagramPosts(tag, config, oAuthResponse);
if(instagramPosts.Data != null)
{
dynamic dyn = JsonConvert.DeserializeObject(instagramPosts.Data.ToString());
foreach (var data in dyn.data)
{
Console.WriteLine("{0} - {1}", data.filter, data.images.standard_resolution.url);
}
}
}
public static string InstagramLogin(InstagramConfig config)
{
var scopes = new List<OAuth.Scope>();
scopes.Add(InstaSharp.OAuth.Scope.Likes);
scopes.Add(InstaSharp.OAuth.Scope.Comments);
string link = InstaSharp.OAuth.AuthLink(config.OAuthUri + "authorize", config.ClientId, config.RedirectUri, scopes, InstaSharp.OAuth.ResponseType.Code);
return link;
}
public static async Task<OAuthResponse> OAuth(string code, InstagramConfig config)
{
// add this code to the auth object
var auth = new OAuth(config);
// now we have to call back to instagram and include the code they gave us
// along with our client secret
return await auth.RequestToken(code);
}
public static async Task<TagResponse> LoadInstagramPosts(String hashTagTerm, InstagramConfig config, OAuthResponse OAuth)
{
var tagApi = new InstaSharp.Endpoints.Tags(config, OAuth);
return await tagApi.Get("soccer");
}
var config=new InstaSharp.InstagramConfig(location.InstagramClientId,location.instagramclientcret,”http://localhost");
字符串instagramLoginLink=InstagramLogin(配置);
GetInstagram(“足球”、配置、instagramLoginLink);
公共静态异步void GetInstagram(字符串标记、InstagramConfig配置、字符串代码)
{
OAuthResponse OAuthResponse=等待OAuth(代码,配置);
var instagrampost=等待加载instagrampost(标记、配置、oAuthResponse);
如果(instagramPosts.Data!=null)
{
dynamic dyn=JsonConvert.DeserializeObject(instagramPosts.Data.ToString());
foreach(动态数据中的var数据)
{
Console.WriteLine(“{0}-{1}”,data.filter,data.images.standard_resolution.url);
}
}
}
公共静态字符串InstagramLogin(InstagramConfig配置)
{
var scopes=新列表();
添加(InstaSharp.OAuth.Scope.Likes);
添加(InstaSharp.OAuth.Scope.Comments);
string link=InstaSharp.OAuth.AuthLink(config.OAuthUri+“authorize”、config.ClientId、config.RedirectUri、scopes、InstaSharp.OAuth.ResponseType.Code);
返回链接;
}
公共静态异步任务OAuth(字符串代码,InstagramConfig配置)
{
//将此代码添加到auth对象
var auth=新的OAuth(配置);
//现在我们必须回拨instagram并加入他们给我们的代码
//还有我们客户的秘密
返回等待身份验证请求令牌(代码);
}
公共静态异步任务LoadInstagramPosts(字符串hashTagTerm、InstagramConfig配置、OAuthResponse OAuth)
{
var tagApi=new InstaSharp.Endpoints.Tags(配置,OAuth);
返回等待tagApi.Get(“足球”);
}
我来晚了一点,但我的回答可能会帮助像我这样的人在谷歌搜索时发现这个问题
您的方法的主要问题是Instagram使用OAuth身份验证。我建议您在OAuth上使用google来理解这些原则,但我将在下面尝试解释它的实际要点
OAuth方法意味着上面代码段中的InstagramLogin
方法的结果不是代码。这是您需要向您的用户发送的链接(是的,使用浏览器或web视图),以便他们可以登录到Instagram帐户,然后授权您的应用程序访问他们的数据(所谓的用户同意屏幕)
最后,在用户同意后,Instagram会将浏览器重定向到您选择的URL(它应该在Instagram API->管理客户端(右上角)->创建/选择要管理的客户端->安全选项卡中的允许重定向URL列表中添加)
您可以尝试设置断点并将instagramLoginLink
的值复制到浏览器的地址框中。您将能够看到认证和同意的整个流程,最后是重定向url,它很可能会在您的浏览器中生成404
最后一个链接将包含get参数中的代码。这就是所谓的授权代码,它允许您获得访问令牌。此代码将从url中提取出来,然后用于调用OAuth.RequestToken
)
PS:是,我上面所说的一切都意味着,你需要一个运行的web应用程序将用户重定向到Instagram,或者一个客户端应用程序将向用户显示一个web视图,并以某种方式处理Instagram将用户发送回重定向url的时刻,以获取代码并继续。编译器是否在抱怨你的代码
LoadInstagramPosts
定义为返回Task
,但不返回任何内容。返回等待tagApi.Get(“足球”)代码>谢谢Crowcoder。这似乎让我回到了调用方法。。。我是否需要跳出异步例程,对返回的内容执行任何实际工作?不,当您等待某个内容时,您可以像对待普通同步调用一样对待它。谢谢Crowcoder。你能看看我编辑的评论吗?如果你有任何建议,请告诉我??我非常了解。我对Instagram API一点也不熟悉,我只是对async/await做了一般性的评论,