instaSharp oauthResponse不起作用

时间:2015-09-15 06:37:13

标签: c# .net instasharp

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

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”类型的异常,但未在用户代码中处理 其他信息:您未经过身份验证

2 个答案:

答案 0 :(得分:0)

您是否已经在Instagram开发者的帐户中注册了测试客户端应用程序? 如果您还没有,请点击您的帐户here,点击顶部按钮"经理客户"并添加您的测试应用程序以获取正确的客户端ID和客户端密码信息。

答案 1 :(得分:0)

使用这种方式,

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

    <!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>

</head>
<body>
<div id="aa" style="position: relative;width: 500px;height: 500px;background-color: #048ca7;z-index: 0">

</div>

<script>
    var i=500;
    var uu=new Array();
    var ii=function(){
        var c=document.getElementById('aa');
        c.innerHTML='';
        var num1,num2;
        var canvas;
        var ctx;
        var str='';
        for(var j=0;j<=i;j++){
            num1=Math.random();
            num2=Math.random();
            str+=("<canvas id='"+j+"' width='100' height='100' style='margin-top:"+num1*400+"px;margin-left:"+num2*400+"px;position: absolute;z-index:1'></canvas>");
        }
        c.innerHTML=str;
        for(j=0;j<=i;j++){
            canvas=document.getElementById(j.toString());
            ctx=canvas.getContext('2d');

            ctx.strokeStyle='black';
            ctx.lineWidth = 2;

            ctx.beginPath();
            ctx.moveTo(10,10);
            ctx.lineTo(80,80);
            ctx.stroke();
        }
    }

</script>
<button style="width: 100px;height: 20px;margin-top: 20px" onclick="ii()">paint</button>
</body>
</html>

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

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

确保您的呼叫功能是异步任务。