2012-07-26 32 views
8

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

+1

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 –

+0

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

Trả lời

6

Tôi tìm thấy một câu hỏi tương tự như tôi: Can a Google Apps Admin manage users files with Drive SDK? mỏ đó đã giúp tôi tìm ra câu trả lời.

Tài liệu cPanel hơi gây hiểu nhầm vì nó đề cập đến việc kích hoạt khóa người dùng và sau đó thêm tên miền vào màn hình quản lý truy cập ứng dụng API. Điều này có vẻ hợp lệ đối với api gdata chứ không phải api Google Drive mới. Bằng cách thêm id ứng dụng như được đề xuất trong câu hỏi khác và cấp quyền truy cập vào phạm vi Drive, giờ đây tôi có thể mạo danh người dùng.

+2

ID ứng dụng khách có giống như xxxxxxxx.apps.googleusercontent.com này không Nếu tôi không tìm thấy id ứng dụng khách ở đâu? – DavidVdd

+1

ID khách hàng có thể được tìm thấy trong giao diện điều khiển api (https://code.google.com/apis/console/) – VoltaicShock

Các vấn đề liên quan