Xamarin 表单 - Instagram 登录:收到无效的 redirect_uri 错误,如何解决? [英] Xamarin form - Instagram login: getting Invalid redirect_uri error, how to fix it?
问题描述
我已经使用 Xamarin.Auth 集成了 Instagram 登录,我提到了:
我终于让它工作了,分享它,因为它可能对寻找解决方案的人有所帮助!
var authenticationator = new OAuth2Authenticator(//ID &您在 Facebook 开发者控制台上创建的应用程序的应用程序秘密 Instagram 基本显示clientId:Keys.InstagramAppId,clientSecret:Keys.InstagramAppSecret,//不再支持基本范围,这将修复无效范围错误:范围:user_profile,user_media",authorizeUrl: new Uri("https://api.instagram.com/oauth/authorize/?client_id=" + Keys.InstagramAppId + "&redirect_uri=https://business.instagram.com/abc&response_type=令牌"),accessTokenUrl: new Uri("https://api.instagram.com/oauth/access_token/"),//确保你的重定向 Url 需要在开发者控制台中注册并且必须是有效的redirectUrl:新的 Uri("https://business.instagram.com/abc")){允许取消 = 真,ShowErrors = 假,ClearCookiesBeforeLogin = true,IsLoadableRedirectUri = false};AuthenticationState.Authenticator = 验证器;authenticationator.Error += (sender, eventArgs) =>{Debug.WriteLine($"Error - {eventArgs.Message}");};authenticationator.Completed += (sender, eventArgs) =>{如果(eventArgs.IsAuthenticated){var userid = eventArgs.Account.Properties[user_id"];var access_token = eventArgs.Account.Properties[access_token"];}};var Presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();演示者.登录(身份验证器);
获取用户信息:
使用:
https://graph.instagram.com/me?fields=id,username&access_token
示例响应:
{id":17841405793187218",用户名":jayposiris"}
注意:我没有办法获取用户的 profile_picture,所以如果有人知道该解决方案,请告诉我.
I have integrate Instagram login using Xamarin.Auth, I have referred: Instagram basic display api - Getting Started and in that first I was getting:
{"error_type": "OAuthException", "code": 400, "error_message": "Invalid scope field(s): basic"}
Then I have got this post: C# Instagram - Invalid Scope
So I have updated everything and I have created New App in developers.facebook.com console under Instagram Basic Display product as mentioned in Instagram Getting Started document.
Then in code I have used that new App's App ID and also changed redirect url, also added Instagram Test users and they have accept invitations too, but now I am getting
{"error_type": "OAuthException", "code": 400, "error_message": "Invalid redirect_uri"}
Auth code:
var authenticator = new OAuth2Authenticator(
clientId: Keys.InstagramClientId,
scope: "basic",
authorizeUrl: new Uri("https://api.instagram.com/oauth/authorize/?client_id=" + Keys.InstagramClientId + "&redirect_uri=https://localhost:3000/callback&response_type=token"),
redirectUrl: new Uri("https://localhost:3000/callback"))
{
AllowCancel = true,
ShowErrors = false,
ClearCookiesBeforeLogin = true
};
AuthenticationState.Authenticator = authenticator;
authenticator.Error += (sender, eventArgs) =>
{
activity.Finish();
};
authenticator.Completed += (sender, eventArgs) =>
{
if (eventArgs.IsAuthenticated)
{
App.AccountType = "Instagram";
App.SaveAccount(eventArgs.Account, view.IsFromReauthentication);
}
};
var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
presenter.Login(authenticator);
Output:
Finally I have make it work, sharing this as it might be help for someone looking for the solution!
var authenticator = new OAuth2Authenticator(
// Id & App Secret of App you have created on FB developer console in Instagram Basic Display
clientId: Keys.InstagramAppId,
clientSecret: Keys.InstagramAppSecret,
// Basic scope is not supported anymore, this will fix Invalid scope error:
scope: "user_profile, user_media",
authorizeUrl: new Uri("https://api.instagram.com/oauth/authorize/?client_id=" + Keys.InstagramAppId + "&redirect_uri=https://business.instagram.com/abc&response_type=token"),
accessTokenUrl: new Uri("https://api.instagram.com/oauth/access_token/"),
// Make sure that your redirect Url needs to register in developer console and it must be valid one
redirectUrl: new Uri("https://business.instagram.com/abc"))
{
AllowCancel = true,
ShowErrors = false,
ClearCookiesBeforeLogin = true,
IsLoadableRedirectUri = false
};
AuthenticationState.Authenticator = authenticator;
authenticator.Error += (sender, eventArgs) =>
{
Debug.WriteLine($"Error - {eventArgs.Message}");
};
authenticator.Completed += (sender, eventArgs) =>
{
if (eventArgs.IsAuthenticated)
{
var userid = eventArgs.Account.Properties["user_id"];
var access_token = eventArgs.Account.Properties["access_token"];
}
};
var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
presenter.Login(authenticator);
To get user information:
Request details using :
https://graph.instagram.com/me?fields=id,username&access_token
Sample Response :
{
"id": "17841405793187218",
"username": "jayposiris"
}
Note: I have not got the way to get user's profile_picture, so if anyone know that solution please let me know.
这篇关于Xamarin 表单 - Instagram 登录:收到无效的 redirect_uri 错误,如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!