2016-11-29 13 views
5

Tôi muốn sử dụng API Google Drive của Java. Tôi đã thử mã này:com.google.api.client.auth.oauth2.TokenResponseException: 401 Không được phép

public Drive getDriveService() throws GeneralSecurityException, IOException, URISyntaxException 
    { 
     HttpTransport httpTransport = new NetHttpTransport(); 
     JacksonFactory JSON_FACTORY = new JacksonFactory(); 

     GoogleCredential credential = new GoogleCredential.Builder() 
      .setTransport(httpTransport) 
      .setJsonFactory(JSON_FACTORY) 
      .setServiceAccountId("[email protected]") 
      .setServiceAccountPrivateKeyFromP12File(new java.io.File("C:\\buffer\\sonora project-3256770463ed.p12")) 
      .setServiceAccountScopes(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN)) 
      .setServiceAccountUser("[email protected]") 
      .build(); 

     Drive service = new Drive.Builder(httpTransport, JSON_FACTORY, null) 
      .setApplicationName("FileListAccessProject") 
      .setHttpRequestInitializer(credential).build(); 

     return service; 
    } 

Nhưng tôi nhận được lỗi này:

An error occurred: com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized 

tôi sử dụng cấu hình này:

enter image description here

Bạn có thể đưa ra một số ý tưởng làm thế nào tôi có thể sửa lỗi này ?

+0

tôi don không nghĩ bạn có thể sử dụng setServiceAccountUser trừ khi tài khoản quản trị miền của nó và bạn đã thêm tài khoản dịch vụ vào tài khoản quản trị và người dùng được đề cập cũng là người dùng trên tài khoản quản trị. – DaImTo

+0

Bạn có thể đưa ra tổng số lỗi và ngoại lệ mà nó đang ném không? – Coder

Trả lời

0

Dựa trên điều này SO question, một lý do khiến bạn gặp lỗi 401 là tài khoản dịch vụ bạn đang sử dụng không có quyền truy cập vào tài khoản ổ đĩa được liên kết với địa chỉ Gmail của bạn. Vì vậy, nếu bạn muốn để có thể truy cập vào các tập tin tải lên, bạn phải cấp cho mình quyền truy cập vào chúng mặc dù tài khoản dịch vụ.

Sau đây là các SO câu hỏi khác mà tôi nghĩ có thể giúp bạn hiểu thêm các tài khoản dịch vụ

+0

Cảm ơn bạn đã trả lời nhưng tôi vẫn không thể tìm thấy giải pháp. Bạn có thể cung cấp thêm một số chi tiết không? –

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