java - 在不提示用户登录的情况下在服务器端验证 Google Drive API

标签 java google-drive-api

情况就是这样。我正在设计一个网站,人们可以在其中创建他们的个人资料。创建的个人资料图片上传到我的谷歌驱动器中,然后使用 Google Drive API 共享。我正在尝试使用 oAuth 2.0 身份验证进行身份验证。但每次,它都会提示登录用户(客户端),并且个人资料图片会上传到他们的谷歌驱动器中。我只需要一次,或者更确切地说是开放式身份验证,以便用户可以直接将他们的照片上传到我的驱动器中..

我在服务器端的代码是这样的...

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();
  }
}

最佳答案

您需要使用服务帐户。参见 https://developers.google.com/accounts/docs/OAuth2ServiceAccount .

从那个页面上说“请求应用程序必须证明自己的身份才能访问 API,而最终用户不必参与。”

关于java - 在不提示用户登录的情况下在服务器端验证 Google Drive API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18608855/

相关文章:

java - 无法从 netbeans 运行 tomcat 7

java - 如何在 Android Studio 中获取 SKU 代码、买家代码和 CSC

java - ClassNotFoundException : net. htmlparser.jericho.来源:新闻通讯

python - 在Google CoLab Notebook中,如何在不进行两次身份验证的情况下从公共(public)Google云端硬盘和我的个人硬盘读取数据?

ios - iOS:如何使用Google云端硬盘SDK库将数据(NSData)上传到特定的Google云端硬盘文档(application/vnd.google-apps.document)

java - println 输出中添加

google-drive-api - Google Drive API 应用程序数据从 Web 浏览器访问凭据(Javascript)

java - 列出并访问 Google 云端硬盘中的 "Incoming"文件夹

oauth-2.0 - OAuth2 中 AuthSub 的 hd param 身份验证相当于什么

java - 按引用实体中的第一项对 JPA 存储库响应进行排序