Forum Xamarin.Forms

Google authentication with oauth2 tutorial

zackie123zackie123 Member ✭✭

Hello guys!

I am using xamarin for crossplatform ios/android about some moths now and also bought some tutorials in udemy about it. While a good portion of my app is completed i am wanted to use google calendar api and google authentication/sign in

I couldn't find a single completed tutorial how to do so and all the solutions i found online are either too complicated without structure or explanations , have details missing or refer to deleted links.

Is there someone who can provide me a good tutorial or example my top priority is to create a good working oauth2 google login and after that i can use i think easily google calendar with rest calls

Any help would be appreciated

here is one sample code i used that is not working

` Task OnLoginClickedAsync(object sender, EventArgs e)
{
string clientId = null;
string redirectUri = null;
string authorizeUri = Constants.AuthorizeUri;
string accessTokenUri = Constants.AuthorizeUri;
switch (Device.RuntimePlatform)
{
/* case Device.iOS:
clientId = Constants.iOSClientId;
redirectUri = Constants.iOSRedirectUrl;
break;*/

            case Device.Android:
                clientId = "152826923349-8n46ad52iub89udn4j7fds43nfmfc416.apps.googleusercontent.com";
                redirectUri = "com.companyname.manager:/oauth2redirect";
                break;
        }

        var authenticator = new OAuth2Authenticator(
            clientId,
            null,
            scope: "email",
            new Uri(authorizeUri),
            new Uri(redirectUri),
            new Uri(accessTokenUri),
            null,
            true);

        authenticator.Completed += OnAuthCompleted;
        authenticator.Error += OnAuthError;

        Xamarin.Auth.Authenticator auth = authenticator;

        var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
        presenter.Login(auth);

    }`

Best Answer

Answers

  • CorwynCorwyn USMember ✭✭✭
    edited June 28

    Have you considered using a service to facilitate your login? PlayFab supports Google authentication (along with other providers.)

  • zackie123zackie123 Member ✭✭

    Actually no it seems interesting i will search it if i can fetch the google account easily from there then i guess i can use later the rest api of calendar for both android/ios. Thank you very much for your help. If anyone else has any tutorial i would be grateful

  • jezhjezh Member, Xamarin Team Xamurai
    edited June 30 Accepted Answer

    For this question, you can read through the following articles
    Google Login Integration in Xamarin.Forms: A Complete Guide
    In above article, we can learn about integrating the Google login into a Xamarin.Forms application using the Xamarin Auth package. And
    Authenticate users through Google with Xamarin.Auth
    In this article, we will learn to integrate Google authentication in our app.

    Hope it can help you.

    Best Regards!

  • zackie123zackie123 Member ✭✭

    Thanx <3

  • zackie123zackie123 Member ✭✭
    edited August 1

    Hello after some summer break decided to progress with my side project. I successfully did google sign in with firebase and after it without firebase. But all the methods i use dont get me the google access token. So i am connected with google but i cannot use other google services api without knowing the access token
    my excepion code
    Google.GoogleApiException: 'Google.Apis.Requests.RequestError Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. [401] Errors [ Message[Invalid Credentials] Location[Authorization - header] Reason[authError] Domain[global] ] '

    my google calendar code

    cser = new CalendarService(new BaseClientService.Initializer { HttpClientInitializer = new CustomUserCredential("I Need the auth2 access token :( "), ApplicationName = "testManager" }); var calendarList = cser.CalendarList.List(); var results = calendarList .Execute();

Sign In or Register to comment.