Tôi muốn mạo danh người dùng và thêm tệp cho người dùng Google Drive thay mặt họ từ quy trình máy chủ. Tôi đã thiết lập một tài khoản dịch vụ và có thể truy cập thành công Drive như tài khoản dịch vụ bổ sung và niêm yết tập tin, vv sử dụng đoạn mã sau:OAuth API Google cho Java không thể mạo danh người dùng
/** Global instance of the HTTP transport. */
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
public static void main(String[] args) {
try {
GoogleCredential credential =
new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("[email protected]")
.setServiceAccountScopes(DriveScopes.DRIVE)
.setServiceAccountPrivateKeyFromP12File(new File("c:/junk/key.p12"))
.build();
Drive drive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build();
drive.files().list().execute();
} catch (Exception e) {
e.printStackTrace();
}
này hoạt động, tuy nhiên chỉ trả về tập tin được gắn liền với những gì tôi giả định được kết hợp với ổ tài khoản dịch vụ (?).
Theo javadoc, GoogleCredential cũng có thể được sử dụng để mạo danh một người sử dụng bằng cách thêm các dịch vụ sử dụng tài khoản địa chỉ email như sau:
GoogleCredential credential =
new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("[email protected]")
.setServiceAccountScopes(DriveScopes.DRIVE)
.setServiceAccountPrivateKeyFromP12File(new File("c:/junk/key.p12"))
.setServiceAccountUser("[email protected]") //<-- impersonate user a
.build();
Tuy nhiên, khi thực hiện mã này, các ngoại lệ sau đây được ném:
com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "access_denied"
}
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:103)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:303)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:323)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:340)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:508)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:260)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:796)
at com.google.api.client.googleapis.json.GoogleJsonResponseException.execute(GoogleJsonResponseException.java:198)
at com.google.api.client.googleapis.services.GoogleClient.executeUnparsed(GoogleClient.java:237)
at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:207)
at com.google.api.services.drive.Drive$Files$List.execute(Drive.java:1071)
Tôi có thiếu cài đặt bước hoặc cấu hình không?
Cảm ơn, David
Bạn đã cấp cho tài khoản dịch vụ đó quyền truy cập vào những người dùng mạo danh trên miền của bạn như thế nào? Bạn đã thêm ID khách hàng và phạm vi thích hợp trong phần OAuth của bảng điều khiển Google Apps chưa? - http://support.google.com/a/bin/answer.py?hl=vi&answer=162106 –
Có đó là phần còn thiếu, cảm ơn câu trả lời Tôi đã tìm thấy câu trả lời của mình từ một câu hỏi tương tự. – David