目前,我想对我的应用程序进行身份验证,并且只通过电子邮件(已经完成)和instagram进行身份验证,我已经从github存储库下载了代码,对我有很大帮助,除了在您要对其进行身份验证的那一刻,它标志着非技术性错误,而不是说“糟糕!发生了错误”的任何内容。
我下载的代码如下:
https://github.com/CrossGeeks/SocialMediaAuthenticationSample
在创建该存储库时保留其代码,显然将重点更多地放在instagram遵循的流程上,并仅修改一些声明为适合我的项目的常量。
const string InstagramApiUrl = "https://api.instagram.com";
const string InstagramScope = "basic";
const string InstagramAuthorizationUrl = "https://api.instagram.com/oauth/authorize/";
const string InstagramRedirectUrl = "https://xamboy.com/default.html";
const string InstagramClientId = "<Client ID>"; ------ my change
这是我尝试使用C#访问instagram的功能
async Task LoginInstagramAsync(AuthNetwork authNetwork)
{
EventHandler<string> onSuccessDelegate = null;
EventHandler<string> onErrorDelegate = null;
EventHandler onCancelDelegate = null;
onSuccessDelegate = async (s, a) =>
{
UserDialogs.Instance.ShowLoading("Loading");
var userResponse = await RestService.For<IInstagramApi>(InstagramApiUrl).GetUser(a);
if (userResponse.IsSuccessStatusCode)
{
var userDataString = await userResponse.Content.ReadAsStringAsync();
//Handling Encoding
var userDataStringFixed = System.Text.RegularExpressions.Regex.Unescape(userDataString);
var instagramUser = JsonConvert.DeserializeObject<InstagramUser>(userDataStringFixed);
var socialLoginData = new NetworkAuthData
{
Logo = authNetwork.Icon,
Picture = instagramUser.Data.ProfilePicture,
Foreground = authNetwork.Foreground,
Background = authNetwork.Background,
Name = instagramUser.Data.FullName,
Id = instagramUser.Data.Id
};
UserDialogs.Instance.HideLoading();
await App.Current.MainPage.Navigation.PushModalAsync(new HomePage(socialLoginData));
}
else
{
//TODO: Handle instagram user info error
UserDialogs.Instance.HideLoading();
await UserDialogs.Instance.AlertAsync("Error","Houston we have a problem" , "Ok");
}
_oAuth2Service.OnSuccess -= onSuccessDelegate;
_oAuth2Service.OnCancel -= onCancelDelegate;
_oAuth2Service.OnError -= onErrorDelegate;
};
onErrorDelegate = (s, a) =>
{
_oAuth2Service.OnSuccess -= onSuccessDelegate;
_oAuth2Service.OnCancel -= onCancelDelegate;
_oAuth2Service.OnError -= onErrorDelegate;
Debug.WriteLine($"ERROR: Instagram, MESSAGE: {a}");
};
onCancelDelegate = (s, a) =>
{
_oAuth2Service.OnSuccess -= onSuccessDelegate;
_oAuth2Service.OnCancel -= onCancelDelegate;
_oAuth2Service.OnError -= onErrorDelegate;
};
_oAuth2Service.OnSuccess += onSuccessDelegate;
_oAuth2Service.OnCancel += onCancelDelegate;
_oAuth2Service.OnError += onErrorDelegate;
_oAuth2Service.Authenticate(InstagramClientId, InstagramScope, new Uri(InstagramAuthorizationUrl), new Uri(InstagramRedirectUrl));
}
访问社交网络的服务。
public class OAuth2Service: IOAuth2Service
{
public event EventHandler<string> OnSuccess = delegate { };
public event EventHandler OnCancel = delegate { };
public event EventHandler<string> OnError = delegate { };
public void Authenticate(string clientId, string scope, Uri authorizeUrl, Uri redirectUrl)
{
var activity = CrossCurrentActivity.Current.Activity;
DroidOAuth2Authenticator auth = new DroidOAuth2Authenticator(
clientId: clientId,// your OAuth2 client id
scope: scope, // the scopes for the particular API you're accessing, delimited by "+" symbols
authorizeUrl: authorizeUrl, // the auth URL for the service
redirectUrl: redirectUrl); // the redirect URL for the service
auth.AllowCancel = true;
auth.ShowErrors = true;
EventHandler<AuthenticatorErrorEventArgs> errorDelegate = null;
EventHandler<AuthenticatorCompletedEventArgs> completedDelegate = null;
errorDelegate = (sender, eventArgs) =>
{
OnError?.Invoke(this, eventArgs.Message);
auth.Error -= errorDelegate;
auth.Completed -= completedDelegate;
};
completedDelegate = (sender, eventArgs) => {
// UI presented, so it's up to us to dimiss it on Android
// dismiss Activity with WebView or CustomTabs
CrossCurrentActivity.Current.Activity.Finish();
if (eventArgs.IsAuthenticated)
{
OnSuccess?.Invoke(this, eventArgs.Account.Properties["access_token"]);
}
else
{
// The user cancelled
OnCancel?.Invoke(this, EventArgs.Empty);
}
auth.Error -= errorDelegate;
auth.Completed -= completedDelegate;
};
auth.Error += errorDelegate;
auth.Completed += completedDelegate;
activity.StartActivity(auth.GetUI(activity));
}
}