首页 文章

在Windows Phone上请求access_token时,Instagram API返回错误

提问于
浏览
0

我一直在尝试将Instagram API集成到我的应用程序中,但我仍然坚持使用身份验证 . 当我刚刚使用隐式流版本时,我让它完全正常工作,它给了我access_token作为URI片段的一部分 . 但是,现在我改为服务器端流程,我在用户登录后收到代码 . 然后我将这些代码发布到访问令牌URL,然后会给我access_token以及有关的某些信息 . 用户,例如他们的用户名和 Profiles 图片链接 .

我正在使用InstaSharp库,修改源代码 .

HttpClient client = new HttpClient { BaseAddress = new Uri(config.OAuthUri + "access_token/", UriKind.Absolute) };
        var request = new HttpRequestMessage(HttpMethod.Post, client.BaseAddress);

        request.AddParameter("client_secret", config.ClientSecret);
        request.AddParameter("client_id", config.ClientId);
        request.AddParameter("grant_type", "authorization_code");
        request.AddParameter("redirect_uri", config.RedirectUri);
        request.AddParameter("code", code);

        return client.ExecuteAsync<OAuthResponse>(request);

创建请求后,其格式如下:{方法:POST,RequestUri:'https://api.instagram.com/oauth/access_token/?client_secret= &client_id = &grant_type = authorization_code&redirect_uri = http://instagram.com&code = ', Version: 1.1, Content: , Headers: { }} (I inserted the space between the redirect_uri and code because it wouldn' t让我发布问题否则)

地址中的所有内容都显示正常,但我总是在重新调整的json文件中收到错误:

“{”code“:400,”error_type“:”OAuthException“,”error_message“:”您必须提供client_id“}”

我不知道造成这个错误的原因 . 任何帮助是极大的赞赏!谢谢!埃利奥特

1 回答

  • 1

    您使用的是最新版本的InstaSharp吗?叉吧here . 您可以在那里查看README.md,尽管's a bit outdated and you need to tweak some config. Here'是如何使用github中的最新版本来完成的:

    // create the configuration in a place where it's more appropriate in your app
    InstaSharpConfig = new InstagramConfig(
        apiURI, oauthURI, clientId, clientSecret, redirectUri);
    
    // then here's a sample method you can have to initiate auth 
    // and catch the redirect from Instagram
    public ActionResult instagramauth(string code)
    {
        if (string.IsNullOrWhiteSpace(code))
        {
            var scopes = new List<InstaSharp.Auth.Scope>();
            scopes.Add(InstaSharp.Auth.Scope.likes);
            var link = InstaSharp.Auth.AuthLink(
                oauthURI, clientId, redirectUri, scopes);
            // where:
            // oauthURI is https://api.instagram.com/oauth
            // clientId is in your Instagram account
            // redirectUri is the one you set in your Instagram account; 
            // for ex: http://yourdomain.com/instagramauth
    
            return Redirect(link);
        }
    
        // add this code to the auth object
        var auth = new InstaSharp.Auth(InstaSharpConfig);
    
        // now we have to call back to instagram and include the code they gave us
        // along with our client secret
        var oauthResponse = auth.RequestToken(code);
    
        // save oauthResponse in session or database, whatever suits your case
    
        // oauthResponse contains the field Access_Token (self-explanatory), 
        // and "User" that'll give you the user's full name, id, 
        // profile pic and username
    
    
        return RedirectToAction("action", "controller");
    }
    

    请注意,您可以拆分“instagramauth”方法 . 这样做是为了简洁 .

相关问题