Search code examples
javagoogle-drive-apigmailgmail-apigoogle-api-client

Create Account Button is always going to browser logged in gmail account. How can it will always ask google for permission?


Project Scenario:

In project Landing page, click on Create New Account --> Set same account which was configured earlier .

Observation:

Redirected to Landing page rather providing any validation message

If you login using first email [email protected]. So it is logged in. So your browser is synced with this gmail account now.

Now, you are going to Create New Account for new email like [email protected]. After clicking, it will go to "Landing Page" again and showing [email protected]. Because your browser is already synced with first logged in account [email protected].

Working Area:

You need to logout after adding new account for removing browser sync.

Code snapshot is given below:

private GoogleAuthorizationCodeFlow authFlow;
HttpServletResponse response;

GoogleAuthorizationCodeRequestUrl url = authFlow.newAuthorizationUrl();
String redirectURL = url.setRedirectUri(CALLBACK_URI).setAccessType("offline").build();
response.sendRedirect(redirectURL);

Question#1:

Is there any other way to always ask google permission window?


Solution

  • I have fixed the issue adding 'setApprovalPrompt("force")' in url.

    String redirectURL = url.setRedirectUri(CALLBACK_URI).setAccessType("offline").setApprovalPrompt("force").build();
    

    I have go through GoogleAuthorizationCodeRequestUrl class documentation. There I have got an option of setting approval prompt.

    i) null --> By default

    ii) auto --> request to auto-approval

    iii) force - to force the approval UI to show.

    Related Documentation is given below:

      /**
       * Sets the approval prompt behavior ({@code "auto"} to request auto-approval or {@code "force"}
       * to force the approval UI to show) or {@code null} for the default behavior of {@code "auto"}.
       *
       * <p>
       * Overriding is only supported for the purpose of calling the super implementation and changing
       * the return type, but nothing else.
       * </p>
       */
      public GoogleAuthorizationCodeRequestUrl setApprovalPrompt(String approvalPrompt) {
        this.approvalPrompt = approvalPrompt;
        return this;
      }