java - Authenticating Google Drive API in server side without prompting users to log in -
so here situation.i designing website,where people can create profile.the profile image created uploaded in google drive , later shared using google drive api.i trying authenticate using oauth 2.0 authentication. each time,it prompts log in users(client side) , profile image gets uploaded in google drive. need 1 time,or rather open authentication users can directly upload pictures in drive..
my code in server side goes this...
package com.gamesquad.uploads; ..//imports done; public class googledriveservices { private static logger log = logger.getlogger(googledriveservices.class); static httptransport httptransport = new nethttptransport(); static jsonfactory jsonfactory = new jacksonfactory(); static properties connectionprop = new properties(); private static googleauthorizationcodeflow flow=null; public static void initiparameteres(){ try { connectionprop.load(thread.currentthread().getcontextclassloader().getresourceasstream("connection.properties")); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } //1.get authorization url public static string authorize(){ flow= new googleauthorizationcodeflow.builder(httptransport, jsonfactory,connectionprop.getproperty("googleappclientid"),connectionprop.getproperty("googleappclientsecret"),arrays.aslist(drivescopes.drive)).setaccesstype("online").setapprovalprompt("auto").build(); string url = flow.newauthorizationurl().setredirecturi(connectionprop.getproperty("googleappredirecturi")).build(); return url; } //2.get authenticated client public static drive createauthorizedclient(string code) throws ioexception{ googletokenresponse response = flow.newtokenrequest(code).setredirecturi(connectionprop.getproperty("googleappredirecturi")).execute(); googlecredential credential = new googlecredential().setfromtokenresponse(response); drive service = new drive.builder(httptransport, jsonfactory, credential).build(); return service; } //3.upload file public static string uploadnewfileingoogledrive(java.io.file inputfile,drive service) throws ioexception,malformedurlexception { //insert file string mimetype="image/"+inputfile.getname().substring(inputfile.getname().lastindexof(".") + 1, inputfile.getname().length()); file body = new file(); body.settitle("profilepic_"+system.currenttimemillis()); body.setdescription("profile picture"); body.setmimetype(mimetype); body.setshared(true); java.io.file filecontent = new java.io.file(inputfile.getabsolutepath()); filecontent mediacontent = new filecontent(mimetype, filecontent); file file = service.files().insert(body, mediacontent).execute(); //file uploaded //share file permission permission = new permission(); permission.setvalue(""); permission.settype("anyone"); permission.setrole("reader"); property newproperty = new property(); newproperty.setvisibility("public"); try { service.permissions().insert(file.getid(), permission).execute(); service.properties().insert(file.getid(), newproperty).execute(); } catch (exception e) { log.error("an error occurred: " + e); } //file shared log.info("file id: " + file.getid()); return file.getid(); } }
you need use service account. see https://developers.google.com/accounts/docs/oauth2serviceaccount.
from page says "the requesting application has prove own identity gain access api, , end-user doesn't have involved."
Comments
Post a Comment