Oauth 在Windows Phone上请求访问令牌时Instagram API返回错误

Oauth 在Windows Phone上请求访问令牌时Instagram API返回错误,oauth,instagram,Oauth,Instagram,我一直在尝试将Instagram API集成到我的应用程序中,但我一直坚持使用身份验证。当我使用隐式流版本时,它工作得非常好,该版本将access_令牌作为URI片段的一部分提供给我。 但是,现在我将更改为服务器端流,在该流中,我在用户登录后收到一个代码。然后,我将此代码发布到访问令牌URL,该URL将为我提供访问令牌以及有关用户的某些信息,例如他们的用户名和个人资料图片链接 我正在使用InstaSharp库,修改源代码 HttpClient client = new HttpC

我一直在尝试将Instagram API集成到我的应用程序中,但我一直坚持使用身份验证。当我使用隐式流版本时,它工作得非常好,该版本将access_令牌作为URI片段的一部分提供给我。 但是,现在我将更改为服务器端流,在该流中,我在用户登录后收到一个代码。然后,我将此代码发布到访问令牌URL,该URL将为我提供访问令牌以及有关用户的某些信息,例如他们的用户名和个人资料图片链接

我正在使用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);
HttpClient client=newhttpclient{BaseAddress=newuri(config.OAuthUri+“access_token/”,UriKind.Absolute)};
var request=newhttprequestmessage(HttpMethod.Post,client.BaseAddress);
request.AddParameter(“client_secret”,config.ClientSecret);
AddParameter(“client_id”,config.ClientId);
request.AddParameter(“授权类型”、“授权代码”);
AddParameter(“redirect_uri”,config.RedirectUri);
请求.添加参数(“代码”,代码);
返回client.ExecuteAsync(请求);
创建我的请求后,其格式如下: {Method:POST,RequestUri:'{CLIENT\u SECRET}&CLIENT\u id={CLIENT\u id}&grant\u type=authorization\u code&redirect\u uri=&code={code}',版本:1.1,内容:,头:{} (我在redirect_uri和代码之间插入了空格,因为它不允许我发布问题)

地址中的所有内容都正常,但我总是在返回的json文件中收到一个错误:

{“code”:400,“error_type”:“OAuthException”,“error_message”:“您必须提供一个客户端id”}

我不知道是什么导致了这个错误。非常感谢您的帮助! 谢谢
Elliott

您是否使用最新版本的InstaSharp?叉子。您可以在那里查看README.md,尽管它有点过时,您需要调整一些配置。以下是如何使用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");
}
//在应用程序中更合适的位置创建配置
InstaSharpConfig=新InstagramConfig(
apiURI、oauthURI、clientId、clientSecret、redirectUri);
//下面是一个示例方法,您可以启动auth
//并捕获Instagram的重定向
公共操作结果instagramauth(字符串代码)
{
if(string.IsNullOrWhiteSpace(代码))
{
var scopes=新列表();
添加(InstaSharp.Auth.Scope.likes);
var link=InstaSharp.Auth.AuthLink(
oauthURI、clientId、重定向URI、作用域);
//其中:
//奥阿图里是https://api.instagram.com/oauth
//clientId在您的Instagram帐户中
//redirectUri是您在Instagram帐户中设置的URI;
//例如:http://yourdomain.com/instagramauth
返回重定向(链接);
}
//将此代码添加到auth对象
var auth=new InstaSharp.auth(InstaSharpConfig);
//现在我们必须回拨instagram并加入他们给我们的代码
//还有我们客户的秘密
var oauthResponse=auth.RequestToken(代码);
//将oauthResponse保存在会话或数据库中,以适合您的情况
//oauthResponse包含字段访问令牌(自解释),
//和“用户”会告诉你用户的全名,id,
//个人资料图片和用户名
返回重定向到操作(“操作”、“控制器”);
}
请注意,您可以拆分“instagramauth”方法。这样做是为了简洁