Nick Ko Nick Ko - 1 month ago 15x
Java Question

Google Drive 403 Forbidden error

there are already a few people that have asked questions in regards to this, but I still wasn't able to solve my problem.

So, I did the gradle/java quickstart to Print the names and IDs for up to 10 files in my Googledrive. That was fine, but I want to write a java script to upload files on my drive, I inserted the main function below in my java file.

public class DriveCommandLine {

/** Application name. */
private static final String APPLICATION_NAME =
"Drive API Java Quickstart";

/** Directory to store user credentials for this application. */
private static final DATA_STORE_DIR = new
System.getProperty("user.home"), ".credentials/drive-java-quickstart");

/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;

/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY =

/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;

/** Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved credentials
* at ~/.credentials/drive-java-quickstart
private static final List<String> SCOPES =

static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
} catch (Throwable t) {

* Creates an authorized Credential object.
* @return an authorized Credential object.
* @throws IOException
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in =
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;

* Build and return an authorized Drive client service.
* @return an authorized Drive client service
* @throws IOException
public static Drive getDriveService() throws IOException {
Credential credential = authorize();
return new Drive.Builder(

public static void main(String[] args) throws IOException{

//Insert a file
Drive service = getDriveService();
File body = new File();
body.setDescription("A test document");
body.setMimeType("text/plain"); fileContent = new"D:\\MyFiles\\nkonstantinidis\\Study\\IntelliJ_Shortcuts.txt");
FileContent mediaContent = new FileContent("text/plain", fileContent);

File file = service.files().insert(body, mediaContent).execute();
System.out.println("file ID: " + file.getId());


And I'm getting an error message shown below when I run it

Exception in thread "main" 403 Forbidden
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Insufficient Permission",
"reason" : "insufficientPermissions"
} ],
"message" : "Insufficient Permission"
at DriveCommandLine.main(

I have created the OAuth client ID and enabled google drive but this still doing it's thing.

Could you maybe help me, or even better direct to tutorial for uploading files on google drive, I'm quite bad in Java so bear with me.

Thanks in advance.


You must change SCOPE used for connection (Insufficient Permission message) into appropriate one

private static final List<String> SCOPES = Arrays.asList(DriveScopes.DRIVE_METADATA_READONLY);

From DriveScope.class:

/** View and manage the files in your Google Drive. */
public static final String DRIVE = "";

/** View and manage its own configuration data in your Google Drive. */
public static final String DRIVE_APPDATA = "";

/** View and manage Google Drive files and folders that you have opened or created with this app. */
public static final String DRIVE_FILE = "";

/** View and manage metadata of files in your Google Drive. */
public static final String DRIVE_METADATA = "";

/** View metadata for files in your Google Drive. */
public static final String DRIVE_METADATA_READONLY = "";

/** View the photos, videos and albums in your Google Photos. */
public static final String DRIVE_PHOTOS_READONLY = "";

/** View the files in your Google Drive. */
public static final String DRIVE_READONLY = "";

/** Modify your Google Apps Script scripts' behavior. */
public static final String DRIVE_SCRIPTS = "";

Remember to delete saved credentials (created by application) after changing SCOPE