Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authenticating Google Drive API in server side without prompting users to log in

So here is the situation.I am designing a website,where people can create their profile.The profile image created is uploaded in my google drive and later shared using Google Drive API.I am trying to authenticate using oAuth 2.0 authentication. But each time,it prompts for log in to the users(client side) and the profile image gets uploaded in their google drive. All i need is a one time,or rather open authentication so that the users can directly upload their pictures in my drive..

My code in server side goes like 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 the 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 a file
public static String uploadNewFileinGoogleDrive(java.io.File inputfile,Drive service) throws IOException,MalformedURLException {
    //Insert a 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 the 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();
  }
}
like image 999
Kaushik Rajbongshi Avatar asked Sep 04 '13 08:09

Kaushik Rajbongshi


1 Answers

You need to use a Service Account. See https://developers.google.com/accounts/docs/OAuth2ServiceAccount.

From that page it says "The requesting application has to prove its own identity to gain access to an API, and an end-user doesn't have to be involved."

like image 146
pinoyyid Avatar answered Sep 28 '22 04:09

pinoyyid