Raj Karri
Raj Karri

Reputation: 551

How to get GoogleApiClient instance in Xamarin?

I want to integrate my xamarin android app with google play services leaderboards and achievements. I am not getting how to convert below code from android documentation to c#.

   // Create the Google Api Client with access to the Play Game and Drive services.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .addApi(Drive.API).addScope(Drive.SCOPE_APPFOLDER) // Drive API
            .build();

    // ...

I tried to convert something like below

GoogleApiClient api = new GoogleApiClient.Builder(this)
           .AddApi(Android.Gms.Games.API)
           .Build();

It's giving error under "Android.Gms.Games.API"

None of the things mentioned in this stack overflow thread are working. Looks like most of these things deprecated.

Please suggest if any other easy way available to integrate with leaderboards.

EDIT: Made changes and now giving below error.

enter image description here

Upvotes: 3

Views: 2180

Answers (1)

SushiHangover
SushiHangover

Reputation: 74184

You will be able to get access to GoogleClientAPI via:

var googleClientAPI = new GoogleApiClient.Builder(Application.Context).AddApi(XXX).Build();

Example using a blocking connect as a quick test:

var client = new GoogleApiClient.Builder(Application.Context)
                                .AddApi(GamesClass.API)
                                .AddScope(GamesClass.ScopeGames)
                                .Build();
Task.Run(() => {
    client.BlockingConnect();
    System.Diagnostics.Debug.WriteLine(client.IsConnected);
});

Note: This assumes that you have registered your app, otherwise you will get a fatal developer error... see Play Connecting


Since you are needing access to leaderboard and achievements, make sure that you have added the packages:

  • Xamarin.GooglePlayServices.Games
  • Xamarin.GooglePlayServices.Identity

These will auto-include the .Base and .Basement package


  • Namespaces to look at after adding the packages:

using Android.Gms.Common.Apis;
using Android.Gms.Games.LeaderBoard;
using Android.Gms.Games.Achievement;

Upvotes: 3

Related Questions