0

我想使用 instaSharp 来使用 instagram api,获取关注者 anp 帖子,并且...当我使用 callbackUrl 获取代码时,我无法发送 Requesttoken(code) 并且我的 oauthResponse 为 null ...这是我的代码:

async Task getaouth()
    {
        var clientId = ConfigurationManager.AppSettings["client_id"];
        var clientSecret = ConfigurationManager.AppSettings["client_secret"];
        var redirectUri = ConfigurationManager.AppSettings["redirect_uri"];
        var realtimeUri = "";

        InstagramConfig config = new InstagramConfig(clientId, clientSecret, redirectUri, realtimeUri);

        Session.Add("InstaSharp.config", config);


        // add this code to the auth object
        var auth = new InstaSharp.OAuth(config);

        // now we have to call back to instagram and include the code they gave us
        // along with our client secret
        var oauthResponse = await auth.RequestToken(code);

        // tell the session that we are authenticated
        //config.isAuthenticated = true;




        Response.Write(r.ToString());

        // both the client secret and the token are considered sensitive data, so we won't be
        // sending them back to the browser. we'll only store them temporarily.  If a user's session times
        // out, they will have to click on the authenticate button again - sorry bout yer luck.
        Session.Add("InstaSharp.AuthInfo", oauthResponse);

        // all done, lets redirect to the home controller which will send some intial data to the app
        //return RedirectToAction("Index", "Home");
        Response.Write("");
    }

在此之后我的 oauthResponse 为空!并在调用此方法 _users.GetSelf() 后得到它: InstaSharp.dll 中发生了“System.InvalidOperationException”类型的异常,但未在用户代码中处理附加信息:您未通过身份验证

4

2 回答 2

0

您是否已经在 Instagram 开发人员的帐户中注册了测试客户端应用程序?如果您还没有,请在此处使用您的帐户登录,单击顶部按钮“管理客户端”并添加您的测试应用程序以获取正确的客户端 ID 和客户端密码信息。

于 2015-11-29T12:33:45.477 回答
0

使用这种方式,

安装最新的 InstaSharp 并执行以下操作:

private InstagramConfig _config;
public async Task< ActionResult> somename(string code)
        {
            if (code != null)
            {
               _config = new InstagramConfig(["InstgramClientId"],
                                                 ["InstgramClientSecret"],
                                                ["InstgramRedirectUrl"]
                                                 );

                var instasharp = new InstaSharp.OAuth(_config);
                var authInfo = await instasharp.RequestToken(code);
                var user = new InstaSharp.Endpoints.Users(_config, authInfo);

                ViewBag.Username = user.OAuthResponse.User.Username;
                ViewBag.Token = authInfo.AccessToken;


                return View();
            }

            return View("name"); 
        }

在您的情况下,您必须调用您的方法,例如:

var authresponse = await getaouth();

确保您的调用函数是异步任务。

于 2015-12-09T09:47:51.743 回答