java - 404 Error when trying to get document metadata via the google drive API -
java - 404 Error when trying to get document metadata via the google drive API -
i authenticating using domain-wide delegation.
i getting google drive service looks this: com.google.api.services.drive.drive@6ebd27b9
this link file i'm trying retrieve: https://docs.google.com/a/rmi.org/document/d/1jrs8slzaad2u4cg-gbdmbvn25dp6f_uuyyl6ermpano/edit
i passing in value file id: 1jrs8slzaad2u4cg-gbdmbvn25dp6f_uuyyl6ermpano
.
when code gets line: file file = service.files().get(fileid).execute();
i error:
an error occured: com.google.api.client.googleapis.json.googlejsonresponseexception: 404 ok { "code" : 404, "errors" : [ { "domain" : "global", "message" : "file not found: 1jrs8slzaad2u4cg-gbdmbvn25dp6f_uuyyl6ermpano", "reason" : "notfound" } ],
when seek find file in question using tool @ bottom of this page, if turn on oauth 2.0, 200 response code , info file in question.
i've looked @ many similar questions on here including this question, can't see wrong in way i've set permissions:
for project, both drive api , drive sdk on under apis , auth. under credentials see client id, email addresss , public key fingerprints i've verified on domain's admin console under security > api reference, enable api access checked. security > advanced settings > manage api client access next scope added: https://www.googleapis.com/auth/drive service account's client id. security > advanced > manage oauth domain key: the "enable consumer key" checkbox checked. there oauth consumer secret code. there no x.509 certificate. the "two legged oauth access command allow access apis" unchecked.here drive service:
public static drive getdriveservice() throws generalsecurityexception, ioexception, urisyntaxexception { httptransport httptransport = new nethttptransport(); jacksonfactory jsonfactory = new jacksonfactory(); googlecredential credential = new googlecredential.builder() .settransport(httptransport) .setjsonfactory(jsonfactory) .setserviceaccountid(service_account_email) //.setserviceaccountscopes(drivescopes.drive) .setserviceaccountscopes(scope) .setserviceaccountprivatekeyfromp12file( new java.io.file(service_account_pkcs12_file_path)) .build(); drive service = new drive.builder(httptransport, jsonfactory, null) .sethttprequestinitializer(credential).build(); homecoming service;
i have these variables defined follows:
/** email of service business relationship */ private static final string service_account_email = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com"; /** path service account's private key file */ private static final string service_account_pkcs12_file_path = "/actualpathhere/howtolisting/war/resources/actualpd12filename.p12"; private static final list<string> scope = arrays.aslist("https://www.googleapis.com/auth/drive.readonly");
here trying file metadata:
private static list<file> retrieveallfiles(drive service) throws ioexception { list<file> result = null; seek { string fileid = "1jrs8slzaad2u4cg-gbdmbvn25dp6f_uuyyl6ermpano"; file file = service.files().get(fileid).execute(); system.out.println("title: " + file.gettitle()); system.out.println("description: " + file.getdescription()); system.out.println("mime type: " + file.getmimetype()); } grab (ioexception e) { system.out.println("an error occured: " + e); } homecoming result; }
can give me hints might going on?
edited
all security reddish herring, turns out. key thing missing valid user's email address. first, functioning code:
import java.io.ioexception; import java.net.url; import java.security.generalsecurityexception; import java.util.arrays; 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.jackson2.jacksonfactory; import com.google.api.services.drive.drive; import com.google.api.services.drive.drivescopes; import com.google.api.services.drive.model.file; public class drivecommandline { private static string service_account_email = "11111-abc123@developer.gserviceaccount.com"; private static final string service_account_pkcs12_file_name = "a_file_name.p12"; private static string service_account_pkcs12_file_path; public static void main(string[] args) throws ioexception { seek { //i did stuff p12 file b/c having problem doing command line (first time) url location = drivecommandline.class.getprotectiondomain().getcodesource().getlocation(); service_account_pkcs12_file_path = location.getfile() + service_account_pkcs12_file_name; drive drive = drivecommandline.getdriveservice("valid-user@example.com"); drivecommandline.printfile(drive, "the_id_of_the_file"); } grab (generalsecurityexception e) { // todo auto-generated grab block e.printstacktrace(); } } //from https://developers.google.com/drive/web/delegation#instantiate_a_drive_service_object /** * build , returns drive service object authorized service accounts * deed on behalf of given user. * * @param useremail email of user. * @return drive service object ready create requests. */ public static drive getdriveservice(string useremail) throws generalsecurityexception, ioexception { httptransport httptransport = new nethttptransport(); jacksonfactory jsonfactory = new jacksonfactory(); googlecredential credential = new googlecredential.builder().settransport(httptransport) .setjsonfactory(jsonfactory) .setserviceaccountid(service_account_email) .setserviceaccountscopes(arrays.aslist(new string[] {drivescopes.drive})) .setserviceaccountuser(useremail) .setserviceaccountprivatekeyfromp12file(new java.io.file(service_account_pkcs12_file_path)) .build(); drive service = new drive.builder(httptransport, jsonfactory, null).sethttprequestinitializer(credential).build(); homecoming service; } // code box @ https://developers.google.com/drive/v2/reference/files/get /** * print file's metadata. * * @param service drive api service instance. * @param fileid id of file print metadata for. */ private static void printfile(drive service, string fileid) { seek { file file = service.files().get(fileid).execute(); system.out.println("title: " + file.gettitle()); system.out.println("description: " + file.getdescription()); system.out.println("mime type: " + file.getmimetype()); } grab (ioexception e) { system.out.println("an error occured: " + e); } } }
the missing line setserviceaccountuser(useremail)
valid scheme user.
with valid user, got file details.
with invalid user, got
an error occured: com.google.api.client.auth.oauth2.tokenresponseexception: 400 bad request { "error" : "invalid_grant", "error_description" : "not valid email." }
and, user did not have access or when line not set, got
an error occured: com.google.api.client.googleapis.json.googlejsonresponseexception: 404 not found { "code" : 404, "errors" : [ { "domain" : "global", "message" : "file not found: the_file_id", "reason" : "notfound" } ], "message" : "file not found: the_file_id" }
note: sure follow each little step @ perform google apps domain-wide delegation of authority since ended getting solution , right keys (didn't need many thought!)
java google-drive-sdk
Comments
Post a Comment