在控制台应用程序中使用Instasharp

4

我有一个 .net 控制台应用程序,我想使用 Instasharp 包装器通过 hashtag 搜索获取 Instagram 帖子。

我广泛使用 C# .net web forms,对 MVC 或如何使用 await 关键字不太熟悉。下面的代码示例似乎可以运行,但从未提供任何输出。

此行:

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

在第一条评论解决了我的初始问题后,我编辑了代码。

我感觉离成功很近,但还有些东西缺失。具体问题请见下文...

我基于 InstaSharp GitHub 的文档编写了代码(https://github.com/InstaSharp/InstaSharp)。GitHub 的示例基于 MVC 应用程序,而我的项目不是 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");
    }

编译器对你的代码有意见吗?LoadInstagramPosts被定义为返回Task<TagResponse>,但你没有返回任何内容。请使用return await tagApi.Get("Soccer");来返回结果。 - Crowcoder
谢谢Crowcoder。似乎确实将我返回到调用方法...我需要移出异步例程才能使用返回的内容进行任何实际工作吗? - Rob Gaudet
不,当你使用await时,你可以像处理普通同步调用一样处理它。 - Crowcoder
谢谢Crowcoder。你能看一下我编辑过的注释并告诉我是否有任何建议吗?我离成功很近了。 - Rob Gaudet
我对Instagram API一点也不熟悉,我只是在谈论async/await的一般情况,但看起来OAuthResponse包含了“code”,所以我不认为它需要成为GetInstagram的参数。 - Crowcoder
1个回答

0

我来晚了,但我的回答可能会帮助那些在谷歌搜索时找到这个问题的人,就像我一样。

你的方法的主要问题是Instagram使用OAuth身份验证。我建议你在谷歌上搜索OAuth以了解其原理,但我将尝试在下面解释它的实际要点。

OAuth方法意味着上面片段中InstagramLogin方法的结果不是代码。它是链接,你需要将用户发送到该链接(是的,使用浏览器或Web视图),以便他们可以登录他们的Instagram帐户,然后授权你的应用程序访问他们的数据(所谓的用户同意屏幕)。

最终,在用户同意之后,Instagram将重定向浏览器到您选择的URL(它应该事先添加到Instagram API允许重定向URL列表中->管理客户端(右上角)->创建/选择要管理的客户端->安全选项卡)

你可以尝试设置断点并将instagramLoginLink的值复制到浏览器的地址框中。你将能够看到整个身份验证和同意流程 - 最后重定向的URL很可能会在你的浏览器中产生404错误。

最后一个链接将包含一个GET参数中的代码。它是所谓的授权码,允许您获得访问令牌。该代码需要从url中提取,然后在调用OAuth.RequestToken时使用。

顺便说一下:是的,我上面所说的一切都意味着您需要运行一个Web应用程序来重定向用户到Instagram,或者是一个客户端应用程序,它将向用户显示一个Web视图,并以某种方式处理Instagram将用户发送回您的重定向URL的时刻-以获取代码并继续进行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接