Я работаю в приложении Xamarin android в c# с аутентификацией Google Firebase. Код работает нормально с firebase. он аутентифицирует пользователя при входе в систему и возвращает имя и адрес электронной почты. Но мне нужен токен доступа после аутентификации в firebase, чтобы продолжить работу с REST API. но я не могу получить токен доступа , могу ли я получить токен доступа?
Код выглядит следующим образом:
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Gms.Auth.Api.SignIn;
using Android.Gms.Common.Apis;
using Android.Gms.Auth.Api;
using Firebase.Auth;
using Firebase;
using Android.Content;
using System;
using Android.Gms.Tasks;
using Java.Lang;
namespace loginGoogle
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity, IOnSuccessListener, IOnFailureListener, IOnCompleteListener
{
GoogleSignInOptions gso;
GoogleApiClient googleApiClient;
FirebaseAuth firebaseAuth;
Button signinButton;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
signinButton = (Button)FindViewById(Resource.Id.signInButton);
signinButton.Click += SigninButton_Click;
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
.RequestIdToken("759281956917-ib3rko81uvs4bvdf3g3p76f0dl7q8k64.apps.googleusercontent.com")
.RequestEmail()
.Build();
googleApiClient = new GoogleApiClient.Builder(this)
.AddApi(Auth.GOOGLE_SIGN_IN_API, gso).Build();
googleApiClient.Connect();
firebaseAuth = GetFirebaseAuth();
UpdateUI();
}
private FirebaseAuth GetFirebaseAuth()
{
var app = FirebaseApp.InitializeApp(this);
FirebaseAuth mAuth;
if (app == null)
{
var options = new FirebaseOptions.Builder()
.SetProjectId("login-bde5d")
.SetApplicationId("login-bde5d")
.SetApiKey("AIzaSyCql6njOSplLxy6Nd2tpNHNSeBxyOm6TQM")
.SetDatabaseUrl("https://login-bde5d.firebaseio.com")
.SetStorageBucket("login-bde5d.appspot.com")
.Build();
app = FirebaseApp.InitializeApp(this, options);
mAuth = FirebaseAuth.Instance;
}
else
{
mAuth = FirebaseAuth.Instance;
}
return mAuth;
}
private void SigninButton_Click(object sender, System.EventArgs e)
{
UpdateUI();
if (firebaseAuth.CurrentUser == null)
{
var intent = Auth.GoogleSignInApi.GetSignInIntent(googleApiClient);
StartActivityForResult(intent, 1);
}
else
{
firebaseAuth.SignOut();
UpdateUI();
}
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == 1)
{
GoogleSignInResult result = Auth.GoogleSignInApi.GetSignInResultFromIntent(data);
if (result.IsSuccess)
{
GoogleSignInAccount account = result.SignInAccount;
LoginWithFirebase(account);
}
}
}
private void LoginWithFirebase(GoogleSignInAccount account)
{
var credentials = GoogleAuthProvider.GetCredential(account.IdToken, null);
firebaseAuth.SignInWithCredential(credentials).AddOnSuccessListener(this)
.AddOnFailureListener(this).AddOnCompleteListener(this, this);
}
public void OnSuccess(Java.Lang.Object result)
{
TextView displayNameText = (TextView)FindViewById(Resource.Id.displaynameText);
TextView emailText = (TextView)FindViewById(Resource.Id.emailText);
displayNameText.Text = "Display Name: " + firebaseAuth.CurrentUser.DisplayName;
emailText.Text = "Email: " + firebaseAuth.CurrentUser.Email;
Toast.MakeText(this, "Login successful", ToastLength.Short).Show();
UpdateUI();
}
public void OnFailure(Java.Lang.Exception e)
{
Toast.MakeText(this, "Login Failed", ToastLength.Short).Show();
UpdateUI();
}
void UpdateUI()
{
if (firebaseAuth.CurrentUser != null)
{
signinButton.Text = "Sign Out";
}
else
{
signinButton.Text = "Sign In With Google";
}
}
public void OnComplete(Task task)
{
//throw new NotImplementedException();
if (task.IsSuccessful)
{
TextView photourlText = (TextView)FindViewById(Resource.Id.photoURLText);
photourlText.Text = "User successfully login with Photo URL: " + firebaseAuth.CurrentUser.PhotoUrl.Path;
}
else
{
TextView photourlText = (TextView)FindViewById(Resource.Id.photoURLText);
photourlText.Text = "problem in user login";
}
}
}
}
I перешли по следующей ссылке: https://www.youtube.com/watch?v=NYMCrD9klA0