Share in google using GTLServicePlus with GIDAuthentication

1k Views Asked by At

Everything works before..

But now that i'm using the new using GoogleSignIn.framework to avoid rejection from apple..


enter image description here


How can i convert GIDAuthentication to GTMFetcherAuthorizationProtocol/GTMOAuth2Authentication to be able to use the GPPNativeShareBuilder?

Update

Sorry for the late update, I successfully created GTMFetcherAuthorizationProtocol/GTMOAuth2Authentication

Using something similar to the answer below by: @Andr3a88 i tried his answer and it is working..

But still, I cannot use GPPNativeShareBuilder for sharing without authenticating using GPPSignIn, here is the error log:

[xxx/0x111533310] [lvl=3] -[GPPNativeShareBuilderImpl open] User must be signed in to use the native sharebox.

The alternative solution that we did was using the GPPSignIn and creating a custom alertview that informs the user that:

"He is about the leave the application" and asks for the users confirmation..

But too bad we cannot submit the app just soon, we've have decided to continue the development then go back to this if our solution wont work and worst possible option is removing G+ sharing...

If you have a solution that allows GPPNativeShareBuilder to work with GIDSignIn, your help will be very appreciated..

Update 2

Adding custom alertview that asks for users confirmation that he's about to leave the application did the work and approved by apple. Cheers! :)

3

There are 3 best solutions below

3
On

Try in this way:

Add to your AppDelegate (or wherever you want) the following property

@property (nonatomic,strong) GTMOAuth2Authentication *authGooglePlus;

Then inside didSignInForUser call setAuthorizerForSignIn, this method is used to create GTMOAuth2Authentication object which will then be used for G+ api calls

- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error
{
    if (error)
        NSLog(@"Status: G+ Authentication error: %@", error);

    [self setAuthorizerForSignIn:signIn user:user];
}

//Add this method used to create GTMOAuth2Authentication
- (void)setAuthorizerForSignIn:(GIDSignIn *)signIn user:(GIDGoogleUser *)user {
    //Create GTMOAuth2Authentication
    GTMOAuth2Authentication *auth = [[GTMOAuth2Authentication alloc] init];
    [auth setClientID:signIn.clientID];
    [auth setClientSecret:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"GoogleClientSecret"]];
    [auth setUserEmail:user.profile.email];
    [auth setUserID:user.userID];
    [auth setAccessToken:user.authentication.accessToken];
    [auth setRefreshToken:user.authentication.refreshToken];
    [auth setExpirationDate: user.authentication.accessTokenExpirationDate];

    //Get app delegate and set GTMOAuth2Authentication
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    appDelegate.authGooglePlus = auth;
}

Now when you make the G+ api call sets the GTMOAuth2Authentication object created during the login

    //Get GTMOAuth2Authentication
    AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    GTMOAuth2Authentication * authGooglePlus = appDelegate.authGooglePlus;
    GTLServicePlus* plusService = [[GTLServicePlus alloc] init];
    plusService.fetcherService.timeout = 30.0;
    [plusService authGooglePlus];
    GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"];
    [plusService executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {

        if (error)
        {
            if(error.code ==  -1001)
            {
                 //Timeout error
            }
        }
        else
        {
             //Do something with your user
        }
}];
2
On

Actually it's quite handy to use google+ APIs with Google sign in.

For GTLServicePlus setAuthorizer, you just need use the following way to get a valid parameter: [plusService setAuthorizer:[GIDSignIn sharedInstance].currentUser.authentication.fetcherAuthorizer];

0
On

After some search, I found that we can use Rest API to get google+ friends along with GIDSignIn ... Just you need to give scopes with GIDSignIn like below:

GIDSignIn *signin = [GIDSignIn sharedInstance];
signin.shouldFetchBasicProfile = true;
signin.scopes = @[@"https://www.googleapis.com/auth/plus.login",@"https://www.googleapis.com/auth/plus.me"];
signin.delegate = self;
signin.uiDelegate = self;
[signin signIn];

Than to get the friend list you need to send GET request tohttps://www.googleapis.com/plus/v1/people/me/people/collection or https://www.googleapis.com/plus/v1/people/userId/people/collection along with google signIn access token.

check this Reference of google+ APIs and

also check Tutorial reference from appcoda

If you want to share than

- (void)showGooglePlusShare:(NSURL*)shareURL {

  // Construct the Google+ share URL
  NSURLComponents* urlComponents = [[NSURLComponents alloc]
      initWithString:@"https://plus.google.com/share"];
  urlComponents.queryItems = @[[[NSURLQueryItem alloc]
      initWithName:@"url"
             value:[shareURL absoluteString]]];
  NSURL* url = [urlComponents URL];

  if ([SFSafariViewController class]) {
    // Open the URL in SFSafariViewController (iOS 9+)
    SFSafariViewController* controller = [[SFSafariViewController alloc]
        initWithURL:url];
    controller.delegate = self;
    [self presentViewController:controller animated:YES completion:nil];
  } else {
    // Open the URL in the device's browser
    [[UIApplication sharedApplication] openURL:url];
  }
}

Reference: https://developers.google.com/+/mobile/ios/share/basic-share