Java 谷歌认证服务帐户
我无法使用生成的服务帐户连接到驱动器API。我已按照本页的说明列表进行操作。然而,当我运行我的代码时,当它将我的文件上传到我的驱动器时,我会得到一个空指针异常。我不确定我在这里做错了什么。我的目标是创建一个命令行应用程序,我可以使用它将文件上传到我的驱动器,而无需任何用户交互。我的代码在下面Java 谷歌认证服务帐户,java,google-api,google-drive-api,google-oauth,Java,Google Api,Google Drive Api,Google Oauth,我无法使用生成的服务帐户连接到驱动器API。我已按照本页的说明列表进行操作。然而,当我运行我的代码时,当它将我的文件上传到我的驱动器时,我会得到一个空指针异常。我不确定我在这里做错了什么。我的目标是创建一个命令行应用程序,我可以使用它将文件上传到我的驱动器,而无需任何用户交互。我的代码在下面 import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.FileContent;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.*;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.drive.model.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
public class DomainAuth {
/** Email of the Service Account */
private static final String SERVICE_ACCOUNT_EMAIL = "972752586307-ecmn7ckdt2o1s3h37canak97d9pa11b7@developer.gserviceaccount.com";
/** Path to the Service Account's Private Key file */
private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "mykey2.p12";
public static void main(String[] args) {
try {
Drive service = getDriveService(SERVICE_ACCOUNT_EMAIL);
//Insert a file
File body = new File();
body.setTitle("My document3");
body.setDescription("A test document");
body.setMimeType("text/plain");
java.io.File fileContent = new java.io.File("document.txt");
FileContent mediaContent = new FileContent("text/plain", fileContent);
File file = service.files().insert(body, mediaContent).execute();
System.out.println("File ID: " + file.getId());
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Build and returns a Drive service object authorized with the service accounts
* that act on behalf of the given user.
*
* @param userEmail The email of the user.
* @return Drive service object that is ready to make requests.
*/
public static Drive getDriveService(String userEmail) throws GeneralSecurityException,
IOException {
java.io.File key = new java.io.File("key.p12");
System.out.println(key.getAbsolutePath());
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(Arrays.asList(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();
return service;
}
}
经过多次搜索,我发现在调用结束时添加的另一个方法解决了我的问题,它是“setApplicationName(“MyAppName”)” 公共静态驱动器getDriveService2(){ HttpTransport HttpTransport=新的NetHttpTransport(); GsonFactory jsonFactory=新的GsonFactory(); 谷歌认证证书; 驱动器服务=null; List scopes=Arrays.asList(DriveScopes.DRIVE); 试一试{ 凭证=新的GoogleCredential.Builder() .setTransport(httpTransport) .setJsonFactory(jsonFactory) .setServiceAccountId(服务\帐户\电子邮件) .setServiceAccountScopes(范围) .SetServiceAccountPrivateKeyfromP12文件( 新的java.io.File(服务\帐户\ PKCS12\文件\路径)) .build(); service=new Drive.Builder(httpTransport,jsonFactory,null).setApplicationName(“MyAppName”) .setHttpRequestInitializer(凭证).build(); }捕获(例外e){ e、 printStackTrace(); } 回程服务; }
您可能希望粘贴堆栈跟踪
public static Drive getDriveService2() {
HttpTransport httpTransport = new NetHttpTransport();
GsonFactory jsonFactory = new GsonFactory();
GoogleCredential credential;
Drive service = null;
List<String> scopes = Arrays.asList(DriveScopes.DRIVE);
try {
credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountScopes(scopes)
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
.build();
service = new Drive.Builder(httpTransport, jsonFactory, null).setApplicationName("MyAppName")
.setHttpRequestInitializer(credential).build();
} catch (Exception e) {
e.printStackTrace();
}
return service;
}