Hi guys am trying to get access token from google using Auth code from spring boot back-end.
But I get the same error response regardless any changes in developer console
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
GET https://www.googleapis.com/drive/v3/files/appfolder
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission: Request had insufficient authentication scopes.",
"reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission: Request had insufficient authentication scopes."
}
Here is my service class:
@Service
public class Google_Auth_service
{
@Value("${auth.server.redirect.url}")
private String redirectUrl;
public String google_auth(String Auth_code) throws IOException {
String CLIENT_SECRET_FILE = "/Users/narashimhaakannan/Downloads/auth_google/src/main/resources/client_secret.json";// json file directory, but it is in the parent folder
String authCode=Auth_code;
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(
JacksonFactory.getDefaultInstance(), new FileReader(CLIENT_SECRET_FILE));
GoogleTokenResponse tokenResponse =
new GoogleAuthorizationCodeTokenRequest(
new NetHttpTransport(),
JacksonFactory.getDefaultInstance(),
"https://oauth2.googleapis.com/token",
clientSecrets.getDetails().getClientId(),
clientSecrets.getDetails().getClientSecret(),
authCode,
redirectUrl)
.execute();
String accessToken = tokenResponse.getAccessToken();
String refreshToken = tokenResponse.getRefreshToken();
String Expiry = String.valueOf(tokenResponse.getExpiresInSeconds());
GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
Drive drive =
new Drive.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
.setApplicationName("Shankar IAS ")
.build();
File file = drive.files().get("appfolder").execute();
GoogleIdToken idToken = tokenResponse.parseIdToken();
GoogleIdToken.Payload payload = idToken.getPayload();
String userId = payload.getSubject(); // Use this value as a key to identify a user.
String email = payload.getEmail();
boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
String name = (String) payload.get("name");
String pictureUrl = (String) payload.get("picture");
String locale = (String) payload.get("locale");
String familyName = (String) payload.get("family_name");
String givenName = (String) payload.get("given_name");
return "{"
+ " \"accessToken\": [{ \""+accessToken+"\"}],\r\n"
+ " \"refreshToken\": [{ \""+refreshToken+"\"}],\r\n"
+ " \"Token_Expires_in\": [{ \""+Expiry+"\"}],\r\n"
+ " \"userId\": [{ \""+userId+"\"}],\r\n"
+ " \"email\": [{ \""+email+"\"}],\r\n"
+ " \"emailVerified\": [{ \""+emailVerified+"\"}],\r\n"
+ " \"name\": [{ \""+name+"\"}],\r\n"
+ " \"pictureUrl\": [{ \""+pictureUrl+"\"}],\r\n"
+ " \"locale\": [{ \""+locale+"\"}],\r\n"
+ " \"familyName\": [{ \""+familyName+"\"}],\r\n"
+ " \"givenName\": [{ \""+givenName+"\"}],\r\n"
+ "}";
}
}
Here is my controller:
@RestController
@RequestMapping("/api/auth")
public class google_auth_controller
{
private final Google_Auth_service service;
public google_auth_controller(Google_Auth_service service) {
this.service = service;
}
@PostMapping("/google")
public String google_auth_controller(@RequestParam String authcode)
{
try {
return service.google_auth(authcode);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
I tried enabling the Google+ and added some scopes at OAuth consent screen, but I get the same error
How can I solve this!