4

I'm trying to write an AppEngine app that writes a Google Document to Google Drive, puts in a specific set of folders and sets access rights. I have this working with the old DocsList API but since that just got deprecated I decided to update my code (and I had some additional functions to add anyway).

Problem I'm facing is this: When I use a service account and try to impersonate a specific user I get a 403 with usageLimits even though I have not used up any of my quota.

Here is the code I'm using:

GoogleCredential credentials = new GoogleCredential.Builder()
                 .setTransport(HTTP_TRANSPORT)
                 .setJsonFactory(JSON_FACTORY)
                 .setServiceAccountId("xxxxxxxxxxgserviceaccount.com")
                 .setServiceAccountScopes(DriveScopes.DRIVE)
                 .setServiceAccountPrivateKeyFromP12File(
                                    new java.io.File("xxxx-privatekey.p12"))
                 .setServiceAccountUser("user@xxxxxx.org").build();

I than use these credentials to initiate my Drive object:

Drive d = Drive.builder(httpTransport, jsonFactory)
               .setHttpRequestInitializer(credentials)
               .setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
                @Override
                public void initialize(JsonHttpRequest request) {
                    DriveRequest driveRequest = (DriveRequest) request;
                    driveRequest.setPrettyPrint(true);
                }
            }).setApplicationName("MYAPPNAME").build();

BTW: I've tried using new Drive(....) but that just won't work, no matter what I try. Keeps throwing errors that internal methods are not found!

Back to this issue: When I than use 'd' to call something like .files().get("SOMEFILEID").execute() I get a 403

{ "code" : 403,
"errors" : [ {
    "domain" : "usageLimits",
    "message" : "Daily Limit Exceeded. Please sign up",
    "reason" : "dailyLimitExceededUnreg",
    "extendedHelp" : "https://code.google.com/apis/console"
  } ],
  "message" : "Daily Limit Exceeded. Please sign up"
}

I can't figure out why this doesn't work. I've look online all day but can't find a suitable answer. Some help is very much appreciated.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • Possible duplicate: http://stackoverflow.com/questions/10323579/google-drive-sdk-java-sample-not-working – Peter Knego Sep 20 '12 at 06:39
  • 1
    I checked that... That is a client side app. I'm having issue with service account, server side. But going through those points once more... who knows, it might work. – Stefan Hogendoorn Sep 20 '12 at 07:04

2 Answers2

2

So pinoyyid's answer did help, although it wasn't the definitive answer.

I ended up solving it like this:

I took my AppID (xxxxx.apps.googleusercontent.com) and added it to my CPanel https://www.google.com/a/cpanel/[[YOURDOMAIN]]/ManageOauthClients with these scopes:

After that I had could do an authenticated request to the Drive environment. Running into a stack of different issues now but the 403 got solved.

  • I'm having the same issue but I don't have a google apps. I just have a foo.appspot.com domain. Any help? – Luca Matteis Oct 03 '12 at 08:23
  • Are you developing this for GMail? Because I don't think there is such a thing as a service account for GMail. If you can provide a bit more info, I might be able to help out. – Stefan Hogendoorn Oct 03 '12 at 13:55
  • Not Gmail. I simply need to use the Drive API. I'm using the `https://www.googleapis.com/drive/v2/files` API. I posted a question over here if you are willing to help me I would be grateful: http://stackoverflow.com/questions/12691677/google-drive-api-through-google-app-engine – Luca Matteis Oct 03 '12 at 14:37
1

I usually get a 403 when the API call was missing the Authorization http header. Trace the http and look at the headers. The rationale for the "quota" message is that without an Auth header, you are anonymous, and the quota for anonymous use is zero.

You might also check that your app is registered for both of the Drive APIs, as I've heard that that can cause the same problem.

On the internal method issue, that sounds like you're using incompatible library versions. What worked best for me was to delete all of the libraries I had downloaded with the sample code, then use the Google Eclipse plugin to "Google/Add Google APIs..." to download all of the latest versions.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • I removed all my other libs and added it through the eclipse plugin. I initially had the same thought: must be old libs. But unfortunatly not. I looked into the header bit but got stuck because to set the header to contain a bearer I need the token but since using a service account, I don't have a token available (or is that the part where I'm wrong?). – Stefan Hogendoorn Sep 20 '12 at 07:05
  • Ok... I've been debugging like crazy today and found that there is not token set. So the requests don't have a beared token. Now all I need to figure out is how to get a token from my service account. – Stefan Hogendoorn Sep 20 '12 at 14:17
  • @pin0yyid How to set header? I have similar problem with new api `https://stackoverflow.com/questions/60704698/how-to-create-google-sheets-using-service-account-credential-in-python` – F. Vosnim Mar 16 '20 at 11:12