Unable to list spreadsheets using Google Drive API in Java - java

I have suffered through the anti-5-minute-quickstart-experience with the Google Drive API, all in an attempt to upgrade my programmatic use of Google Spreadsheets to be OAuth2 compliant (yes, I know, I was a straggler).
I'm now at the point where I'm simply trying to list the spreadsheets I have on Drive and I get nothing - no data, but also no errors. Drive seems to be happy, but I'm not.
Would very much appreciate any help...
Here's the details:
Yes, I do have spreadsheets (several) in my Google Drive. When I am logged in via web browser, I see references to them at: https://spreadsheets.google.com/feeds/spreadsheets/private/full
I have created a Google Credential for an installed application. Code for programmatically uploading a Drive Doc (from Google's "quickstart") worked fine.
The following code runs just fine, but claims I have no spreadsheets. I'm confused as to why.
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.drive.DriveScopes;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
public class Spread {
public static void main(String[] args) throws Exception {
ArrayList scopes = new ArrayList();
scopes.add(0, DriveScopes.DRIVE);
scopes.add(1, "https://spreadsheets.google.com/feeds");
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.setServiceAccountId(
"my-service-email-address#developer.gserviceaccount.com")
.setServiceAccountPrivateKeyFromP12File(new File("/path/to/my/P12/file"))
.setServiceAccountScopes(scopes).build();
credential.refreshToken();
SpreadsheetService service = new SpreadsheetService("tmp");
service.setOAuth2Credentials(credential);
// Define the URL to request. This should never change.
URL SPREADSHEET_FEED_URL = new URL(
"https://spreadsheets.google.com/feeds/spreadsheets/private/full");
// Make a request to the API and get all spreadsheets.
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,
SpreadsheetFeed.class);
List spreadsheets = feed.getEntries();
// Iterate through all of the spreadsheets returned
int i = 0;
for (SpreadsheetEntry spreadsheet : spreadsheets) {
++i;
System.out.println(spreadsheet.getId());
}
System.out.println("Done " + i);
}
}
The end result of running this code is "Done, 0 sheets found."
Help!

Related

*401 Unauthorized* When following tutorial Java implementation of Video.insert by developers.google.com

I am trying to upload an mp4 file to my youtube channel using the YouTube Data API, however, I keep getting a 401 Unauthorized back.
Using the google console cloud I created an (unrestricted) API key. Afterwards, I enabled YouTube Data API v3
I then copied the java code snippet from the use cases documentation for making a Video.Insert call to their services.
The things I changed from the snippet (required):
I used the aforementioned API key to replace "YOUR_API_KEY"
And also replaced new File("YOUR_FILE") for obvious reasons. The file is a 17MB mp4 file.
Because the package provided with the snippet no longer comes with com.google.api.client.json.jackson2.JacksonFactory; Following this solution I included the dependency:
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson</artifactId>
<version>1.29.2</version>
</dependency>
And I replaced JacksonFactory.getDefaultInstance() with new JacksonFactory()
Finally, as youtubeService.videos().insert(args) doesn't support a string as its first argument I replaced the string with a List.of() of the CSV string. I've also tried a List with each of the comma-seperated strings as items in the list but that doesn't seem to change the outcome either.
This results in the following request: https://youtube.googleapis.com/upload/youtube/v3/videos?key=MyKeyAware&part=snippet&part=status&uploadType=resumable
For good measure, here are the relevant dependencies I'm using (excluding the aforementioned dependency):
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.35.1</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-youtube</artifactId>
<version>v3-rev20220612-1.32.1</version>
</dependency>
I would really appreciate some help as I've been struggling with this issue for a while now!
Code:
package nl.phi.ysg.service.YoutubeApi;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.InputStreamContent;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.Video;
import com.google.api.services.youtube.model.VideoSnippet;
import com.google.api.services.youtube.model.VideoStatus;
import org.springframework.core.io.ClassPathResource;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.List;
public class YoutubeApiServiceImpl implements YoutubeApiService {
private static final String DEVELOPER_KEY = "...";
private static final String APPLICATION_NAME = "YSG";
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
/**
* Build and return an authorized API client service.
*
* #return an authorized API client service
* #throws GeneralSecurityException, IOException
*/
public static YouTube getService() throws GeneralSecurityException, IOException {
final NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
return new YouTube.Builder(httpTransport, JSON_FACTORY, null).setApplicationName(APPLICATION_NAME).build();
}
public static void main(String[] args) throws GeneralSecurityException, IOException, GoogleJsonResponseException {
YouTube youtubeService = getService();
// Define the Video object, which will be uploaded as the request body.
Video video = new Video();
// Add the snippet object property to the Video object.
VideoSnippet snippet = new VideoSnippet();
snippet.setCategoryId("22");
snippet.setDescription("Description of uploaded video.");
snippet.setTitle("Test video upload.");
video.setSnippet(snippet);
// Add the status object property to the Video object.
VideoStatus status = new VideoStatus();
status.setPrivacyStatus("private");
video.setStatus(status);
File mediaFile = new ClassPathResource("test/test.mp4").getFile();
InputStreamContent mediaContent = new InputStreamContent("application/octet-stream", new BufferedInputStream(new FileInputStream(mediaFile)));
mediaContent.setLength(mediaFile.length());
// Define and execute the API request
YouTube.Videos.Insert request = youtubeService.videos().insert(List.of("snippet,status"), video, mediaContent);
Video response = request.setKey(DEVELOPER_KEY).execute();
System.out.println(response);
}
}
It's unclear from your question but the error indicates that you're not including User authentication (OAuth) and this is required by the YouTube API:
See the Quickstart specifically creating OAuth client ID in step 2b.
The API key is used to authenticate your app.
The OAuth2 client ID is used to authenticate human users.

When using service account impersonation, when calling export on Google Docs using v3 API, viewedByMeTime timestamp is updated

I am using a service account to access google doc files of users in my enterprise google account using impersonation.
See:
https://developers.google.com/drive/api/v3/about-auth#OAuth2Authorizing
So far so good.
Then, I need to download contents of Google Docs.
When calling Google Drive API to download the contents of a Google Doc, the documentation says to run the following:
https://developers.google.com/drive/api/v3/manage-downloads
Here is a java program that should reproduce the problem:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.SecurityUtils;
import com.google.api.services.drive.Drive;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;
public class FetchGoogleDocContentsWithServiceAccount {
static int readTimeout = 60000;
static int connectTimeout = 60000;
static String serviceAccountId = "";
static String serviceAccountEmail = "";
static String serviceAccountPrivateKeyFile = "";
static String serviceAccountPrivateKeyFilePassword = "";
static String fileId = "";
static JacksonFactory jacksonFactory = new JacksonFactory();
static NetHttpTransport httpTransport = new NetHttpTransport();
static List<String> googleScopeList = Arrays.asList("https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/admin.directory.group.readonly",
"https://www.googleapis.com/auth/admin.directory.user.alias.readonly",
"https://www.googleapis.com/auth/admin.directory.group", "https://www.googleapis.com/auth/admin.directory.user",
"https://www.googleapis.com/auth/drive");
public static void main(String[] args) throws Exception {
Drive drive = (new Drive.Builder(httpTransport,
jacksonFactory,
getRequestInitializer(getGoogleCredentials())))
.setApplicationName("Sample app").build();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
drive.files().export(fileId, "application/vnd.google-apps.document")
.executeMediaAndDownloadTo(baos);
System.out.println(baos.toString("UTF-8"));
}
public static HttpRequestInitializer getRequestInitializer(final GoogleCredential requestInitializer) {
return httpRequest -> {
requestInitializer.initialize(httpRequest);
httpRequest.setConnectTimeout(readTimeout);
httpRequest.setReadTimeout(connectTimeout);
};
}
public static GoogleCredential getGoogleCredentials() {
GoogleCredential credential;
try {
GoogleCredential.Builder b = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jacksonFactory).setServiceAccountId(serviceAccountId)
.setServiceAccountPrivateKey(SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
new FileInputStream(new File(serviceAccountPrivateKeyFile)), serviceAccountPrivateKeyFilePassword,
"privatekey", serviceAccountPrivateKeyFilePassword))
.setServiceAccountScopes(googleScopeList);
if (serviceAccountEmail != null) {
b = b.setServiceAccountUser(serviceAccountEmail);
}
credential = b.build();
} catch (IOException | GeneralSecurityException e1) {
throw new RuntimeException("Could not build client secrets", e1);
}
return credential;
}
}
When I have performed this operation, we are seeing that the viewedByMeTime field is actually being updated as the impersonated user.
This is not good, because now people think someone might have stolen access to their account. They are going to open tickets with the security team.
Is this expected? How can I make this stop? Is there another method in the API I can call to download the google docs without updating this timestamp?
Also opened a ticket on the github for the google drive java sdk: https://github.com/googleapis/google-api-java-client-services/issues/3160
Updating the viewedByMeTime field upon calling the endpoint is indeed intended behaviour. Any action performed through the API is considered the same way as if the user did that action manually (i.e. that field would be updated too when the user visits the document through the UI).
By using domain-wise delegation (or "user impersonation"), you have no way to avoid this issue.
The only workaround would be to give the service account access to this file, and let it export the file without domain-wide delegation. The viewedByMeTime field will be updated only for the service account itself, but not for the original owner of that file (or any other user having access to it).

Access to Google Drive Spreadsheet from remote Server

I have client/server application. The server side must access a Spreadsheet on my own Google Drive.I make a test class to try to access to the spreadsheet list:
package com.eng.app;
import java.io.File;
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.util.ServiceException;
public class TestGoogleCredential {
public static void main(String[] args) throws IOException,
InterruptedException, GeneralSecurityException, URISyntaxException, ServiceException {
List<String> SCOPES = Arrays
.asList("https://spreadsheets.google.com/feeds");
final HttpTransport TRANSPORT = new NetHttpTransport();
final JsonFactory JSON_FACTORY = new JacksonFactory();
File cert = new File(Credential.class.getClassLoader()
.getResource("<the_file>.p12").toURI());
HttpTransport transport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(transport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(<email-address>)
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(cert).build();
SpreadsheetService service = new SpreadsheetService("Inertia");
service.setOAuth2Credentials(credential);
String urlString = "https://spreadsheets.google.com/feeds/spreadsheets/private/full";
URL SPREADSHEET_FEED_URL = new URL(urlString);
// istantiate the feed to the spreadsheets
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,SpreadsheetFeed.class);
// the list of all spreadsheets
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
System.out.println(feed.getEntries().size());
// select the particular spreadsheet on Inertia Ontology
for (SpreadsheetEntry spreadsheetEntry : spreadsheets) {
System.out.println("founded");
}
}
}
But when I run the code the size is 0 and no spreadsheet is printed out.
I have also make another test coding a program that access (on localhost) the Spreadsheet list using my Google Credential instead the OAuth2 authorization and it works well (it prints out all my spreadsheet on my Google Drive).
service.setUserCredentials(username, password); //My personal Google Account
NB: when I deploy on a remote server the test-program with My personal Google Credential (not OAuth2), it was blocked by Google for security policies.
Please any ideas?
Thank you
try to modify this snippet
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(transport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("google-dev-console-email-address")
.setServiceAccountUser("your-gmail")
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(cert).build();

Java Google Spreadsheet API: Updating Public Worksheet 'java.lang.UnsupportedOperationException: Entry cannot be updated'

I am quite new to Java and new to the Google Data API's. I am trying to change the size of a worksheet in a publicly shared spreadsheet. I'm using the following code:
import com.google.gdata.client.authn.oauth.*;
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.*;
import com.google.gdata.data.batch.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.*;
import java.util.*;
static void writeToGoogleSpreadsheet(String spreadsheetKey) throws IOException, ServiceException {
SpreadsheetService service = new SpreadsheetService("com.example");
String urlString = "https://spreadsheets.google.com/feeds/worksheets/" + spreadsheetKey + "/public/basic";
URL url = new URL(urlString);
try {
WorksheetFeed worksheetFeed = service.getFeed(url, WorksheetFeed.class);
List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
WorksheetEntry worksheet = worksheets.get(0);
System.out.println(worksheet.getTitle().getPlainText());
System.out.println(worksheet.getCanEdit());
// Update the local representation of the worksheet.
worksheet.setTitle(new PlainTextConstruct("Updated Worksheet"));
worksheet.setColCount(40);
worksheet.setRowCount(40);
// Send the local representation of the worksheet to the API for
// modification.
worksheet.update();
} finally{}
}
The console displays the correct worksheet title and size, so I'm pretty sure I am accessing the right worksheet. However, worksheet.update() throws the following exception:
Exception in thread "main" java.lang.UnsupportedOperationException: Entry cannot be updated
at com.google.gdata.data.BaseEntry.update(BaseEntry.java:635)
atcom.example.GoogleSpreadsheetCommunicator.writeToGoogleSpreadsheet(GoogleSpreadsheetCommunicator.java:119)
at com.example.Main.guildSim(Main.java:114)
at com.example.Main.main(Main.java:73)
Does anyone know what I am doing wrong?
Thanks for reading and kind regards,
Karel
You can't edit a public feed,
change urlString to use .../private/...
and you will need to use one of the 3 ways to authenticate you can find on https://developers.google.com/google-apps/spreadsheets/#authorizing_requests_with_clientlogin

So when I deploy my code to CloudBees global.java won't run (Play Framework 2.1)

I'm just toying around with the google drive api (cloud storage) and figuring out how things work, I wrote some code in play framework where in global.java onStart access to the drive is gained and a test document is written to the drive. Locally this works fine but when I deploy my code to a new CloudBees app and visit the app I get this error in my log and I just can't figure out how to debug it:
Play server process ID is 7454
Oops, cannot start the server.
#6eonea90e: Cannot init the Global object
at play.api.WithDefaultGlobal$$anonfun$play$api$WithDefaultGlobal$$globalInstance$1.apply(Application.scala:57)
at play.api.WithDefaultGlobal$$anonfun$play$api$WithDefaultGlobal$$globalInstance$1.apply(Application.scala:51)
at play.utils.Threads$.withContextClassLoader(Threads.scala:18)
at play.api.WithDefaultGlobal$class.play$api$WithDefaultGlobal$$globalInstance(Application.scala:50)
at play.api.DefaultApplication.play$api$WithDefaultGlobal$$globalInstance$lzycompute(Application.scala:383)
at play.api.DefaultApplication.play$api$WithDefaultGlobal$$globalInstance(Application.scala:383)
at play.api.WithDefaultGlobal$class.global(Application.scala:66)
Link to complete CloudBees log
This is the code of Global.java:
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.Drive.Files;
import com.google.api.services.drive.model.FileList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import play.*;
import play.Logger;
public class Global extends GlobalSettings {
private static String CLIENT_ID = "595172328396-l4kpto8ip9fpaea0k2987eeq8f42bged.apps.googleusercontent.com";
private static String CLIENT_SECRET = "EvTUvAodjGx2eW_d3k8oy8Fb";
private static String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
#Override
public void onStart(Application app) { try{
Logger.info("Application has started");
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.setJsonFactory(jsonFactory).setTransport(httpTransport).build()
.setRefreshToken("1/MZ4GTNA_HMbOcKDSqp6ymSd11dlkgxoMXxfWwhwMJRg").setAccessToken("ya29.AHES6ZQk7NDC-OCba7_yANc_uqWPLwDLl95TlT_DXgkLqrr6qmyLRw");;
//Create a new authorized API client
Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build();
//Insert a file
File body = new File();
body.setTitle("New Document");
body.setDescription("A test document");
body.setMimeType("text/plain");
java.io.File fileContent = new java.io.File("document.txt");
FileContent mediaContent = new FileContent("text/plain", fileContent);
File file = service.files().insert(body, mediaContent).execute();
Logger.info("List of stuff: " + service.children().toString());
Logger.info("File ID: " + file.getId()); }catch (IOException ex) {}
}
}
The google access token is only good for 1 hour. It does not seem as if you are actually requesting an access token with your refresh token... Instead it seems like you are setting one that was already issued.
You need to request an access token be fetched (using the refresh token)
The "fun" is that once you use the refresh token to generate a new access token, the old ones are invalidated... Or at least that is the experience I have found...

Categories

Resources