How can I use Google's people API in Xamarin.iOS ?

sumitmishrasumitmishra Member ✭✭✭

Hi,
I have to access Google contact in our Xamarin.ios app so, We have generate Google's people API key and client id in https://console.developers.google.com/
But I don’t know how to consume API in Xamarin.ios app. We need to get Google contact details using Api.
We got some code for .net in Google documentation but unfortunately it’s not working in Xamarin.ios platform.
Please help me to solve this issue.

Best Answers

Answers

  • JohnHJohnH GBMember ✭✭✭✭✭

    The first step I would take is getting it working using Postman, to validate your request and see the response. Maybe try that?

  • sumitmishrasumitmishra Member ✭✭✭

    Hi @JohnH ,
    I have to consume it in xamarin.ios app using Google OAuth 2.0. Unfortunately Google API library is not supporting for Xamarin platforms.

    Is any other way to consume Google People Rest API using OAuth 2.0 authentication in Xamarin.ios?

  • LandLuLandLu Member, Xamarin Team Xamurai

    You could find the Google api for Xamarin iOS here:
    https://github.com/xamarin/GoogleApisForiOSComponents

  • sumitmishrasumitmishra Member ✭✭✭
    edited February 7

    Hi @LandLu ,
    We need Google Cloud Platform APIs but this not available in the list that you have suggested.
    Please check the link that I have required and provided in Google docs
    https://github.com/googleapis/google-api-dotnet-client

  • LandLuLandLu Member, Xamarin Team Xamurai

    The link is broken.
    The Xamarin.Firebase.iOS.Auth is included.
    Do you mean this cloud api:
    https://www.nuget.org/packages/Xamarin.Google.Cloud.Platform.Api.iOS/

  • LandLuLandLu Member, Xamarin Team Xamurai

    It claimed clearly that it didn't support Xamarin: https://github.com/googleapis/google-api-dotnet-client#unsupported-frameworks
    So we need to find a library that supports Xamarin iOS.
    Could the library I posted above help you complete the authentication?

  • sumitmishrasumitmishra Member ✭✭✭

    We have to access Google contact data like email address and mobile number etc but this is not included in it's feature.
    According to Google documentation, we had created Google People API Key and client id that provides these data.
    Could you please send me any sample code to use this library.

  • LandLuLandLu Member, Xamarin Team Xamurai
    edited February 7

    You could try the sign in sample I posted above:
    https://github.com/xamarin/GoogleApisForiOSComponents/tree/master/samples/Google/SignIn/SignInExample
    Get it like:

    SignIn.SharedInstance.SignedIn += (sender, e) => {
        // Perform any operations on signed in user here.
        if (e.User != null && e.Error == null) {
            statusText.Text = string.Format ("Signed in email: {0}",    e.User.Profile.Email);
            ToggleAuthUI ();
        }
    };
    
  • sumitmishrasumitmishra Member ✭✭✭

    This library is not working. I have added it to my application but it is showing error like please check screen shot.
    I found This package has been deprecated as it is legacy and is no longer maintained.
    https://nuget.org/packages/Xamarin.Google.Cloud.Platform.Api.iOS/

  • LandLuLandLu Member, Xamarin Team Xamurai
  • sumitmishrasumitmishra Member ✭✭✭

    This is not working properly. It's just checking user has logged in or not using this code and enable disable button.

    void ToggleAuthUI()
            {
                if (SignIn.SharedInstance.CurrentUser == null || SignIn.SharedInstance.CurrentUser.Authentication == null)
                {
                    // Not signed in
                    lblStatus.Text = "Google Sign in\niOS Demo";
                    signInButton.Hidden = false;
                    signOutButton.Hidden = true;
                    disconnectButton.Hidden = true;
                }
                else
                {
                    // Signed in
                    lblStatus.Text = string.Format("Signed in user: {0}", SignIn.SharedInstance.CurrentUser.Profile.Name);
                    signInButton.Hidden = true;
                    signOutButton.Hidden = false;
                    disconnectButton.Hidden = false;
                }
    }
    

    My requirement totally different. We need a google prompt where user input id and password and allow access to fetch contect.

  • LandLuLandLu Member, Xamarin Team Xamurai

    I'm confused about what you want to achieve.
    If you want to retrieve the email of the user from the auth api of Google this documentation has a specific description:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/authentication/oauth
    When you set the scope to email:
    https://github.com/xamarin/Xamarin.Auth/blob/master/samples/Xamarin.Forms/someone1984-Xamarin.Auth-sample/AuthExample/AuthExample/AuthExample/OAuth/GoogleConfiguration.cs
    We could read the corresponding information of the user.
    If you want to consume other apis the components github has illustrated the samples.

  • sumitmishrasumitmishra Member ✭✭✭
    edited February 13

    Hi @LandLu ,
    I have implemented Authenticate Users with an Identity Provider with google using OAuth2Authenticator
    and it is working to open Google prompt to login . But after successful login this in not redirect to my app.
    The Auth.Completed += Auth_Completed; event is not calling.

    Please check my code and help me. And After redirect we need to get email from contact of that users.

     public override void ViewDidLoad()
            {
                base.ViewDidLoad();
    
                Auth = new OAuth2Authenticator(
                Configuration.ClientId,
                string.Empty,
                "https://www.googleapis.com/auth/drive",  // we need contact here?
                new Uri("https://accounts.google.com/o/oauth2/v2/auth"),
                new Uri(Configuration.RedirectUrl),  //this is "com.MyApp.GetContactDemo:/oauth2redirect"
                new Uri("https://www.googleapis.com/oauth2/v4/token"),
                isUsingNativeUI: true);
    
                Auth.Completed += Auth_Completed;
                Auth.Error += Auth_Error;
    
            }
    
     private  void  Auth_Completed(object sender, AuthenticatorCompletedEventArgs e)
            {
                // SFSafariViewController doesn't dismiss itself
                DismissViewController(true, null);
    
                if (e.IsAuthenticated)
                {
                    // The user is authenticated
                    // Extract the OAuth token
                    var token = new GoogleOAuthToken
                    {
                        TokenType = e.Account.Properties["token_type"],
                        AccessToken = e.Account.Properties["access_token"]
                    };
    
                    // Do something
                }
                else
                {
                    // The user is not authenticated
                }
            }
    
  • sumitmishrasumitmishra Member ✭✭✭

    Hi @LandLu ,
    I have done all the things according to documentation . I have set the URL Types in info.plist but OpenUrl is not being called from AppDelegate class and after authentication this message displayed "safari cannot open the page because the address is invalid"

    public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
            {
                // Convert iOS NSUrl to C#/netxf/BCL System.Uri - common API
                var uri_netfx = new Uri(url.AbsoluteString);
    
                clsXamrinAuthSample.Auth.OnPageLoading(uri_netfx);
    
                return true;
            }
    

    Please help.

  • LandLuLandLu Member, Xamarin Team Xamurai

    Please check your redirect address. It seems you haven't used the valid address.
    Follow the format as I posted above depending on the Google rules.
    Moreover, use this event:

    public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
    {
        return base.OpenUrl(app, url, options);
    }
    

    Your event has been deprecated.

  • sumitmishrasumitmishra Member ✭✭✭
    edited February 19

    Yes I have configure it according to google rule.

    I am using "com.googleusercontent.apps.902730282010-ks3kd03ksoasioda93jldas93jjj22kr:/oauth2redirect" as a redirect Url in OAuth2Authenticator. Can you please show me the format of redirect Url.
    I have modified the OpenUrl method also but something wrong in my redirect url and this method is not being called yet.

  • sumitmishrasumitmishra Member ✭✭✭
    edited February 20

    Thank you @LandLu . Finally It is working now. :)

  • LandLuLandLu Member, Xamarin Team Xamurai
    If it helped, do not forget to mark it as an answer.
  • sumitmishrasumitmishra Member ✭✭✭
    edited February 27

    Hi @LandLu ,
    We need some help in this thread because OpenUrl() method is not being called in iOS 13 or later version but it's worked in iOS 12.
    It is also not working in simulator (iOS 13.3).

     public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
            {
                // Convert iOS NSUrl to C#/netxf/BCL System.Uri - common API
                var uri_netfx = new Uri(url.AbsoluteString);
    
                clsXamrinAuthSample.Auth.OnPageLoading(uri_netfx);
    
                return true;
            }
    

    Please help as soon as possible.

  • sumitmishrasumitmishra Member ✭✭✭

    Hi, I have implemented OpenUrlContexts() method in Scene Delegate and being called also but I am unable to access Url property. Please check my code below. I am just close to my solution.

          [Export("scene:openURLContexts:")]
           public void OpenUrlContexts(UIScene scene, NSSet<UIOpenUrlContext> urlContexts)
                {
                foreach (var urlContextData in urlContexts)
                    {
                      var urlString = urlContextData.Url;  //not found url but it is showing in debug. some conversion issue
                      clsXamrinAuthSample.Auth.OnPageLoading(urlString);
                    } 
                }
    
Sign In or Register to comment.