我想上傳一個文件到我的帳戶在谷歌驅動器。這個過程應該沒有人爲干預。有沒有辦法上傳一個文件在我的谷歌驅動器使用谷歌驅動器API提供我的用戶和我的密碼?在沒有OAuth的情況下將文件上傳到java驅動器
什麼我迄今所做的:
- 我https://console.developers.google.com創建了一個項目,並啓用谷歌驅動API
- 在https://console.developers.google.com - >的API &驗證 - >證書我創建了一個新的客戶端ID,在應用程序類型I中選擇服務帳戶,然後我下載了pkcs12密鑰。
我有下面的代碼:
/** * Email of the Service Account */ private static final String SERVICE_ACCOUNT_EMAIL = "[email protected]"; /** * Path to the Service Account's Private Key file */ private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "/path/to/pkcs12/donloaded.pkcs12"; /** * Build and returns a Drive service object authorized with the service * accounts. * * @return Drive service object that is ready to make requests. */ public static Drive getDriveService() throws GeneralSecurityException, IOException, URISyntaxException { HttpTransport httpTransport = new NetHttpTransport(); JacksonFactory jsonFactory = new JacksonFactory(); List<String> scopes = new ArrayList<String>(); scopes.add(DriveScopes.DRIVE); GoogleCredential credential; 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(); Drive service = new Drive.Builder(httpTransport, jsonFactory, null) .setHttpRequestInitializer(credential).build(); return service; } public static void main(String[] args) throws GeneralSecurityException, IOException, URISyntaxException { Drive client = getDriveService(); File body = new File(); body.setTitle("mytitle"); body.setMimeType("application/vnd.google-apps.spreadsheet"); File file = client.files().insert(body).execute(); System.out.println(file.getId()); System.out.println(file.getAlternateLink()); }
當我運行代碼的結果是正確的,我得到的文件的id和備用鏈路,但該文件無法加載到我的帳戶。我將備用鏈接粘貼到瀏覽器中,但我沒有權限。
哪裏文件剛上載?我應該更改什麼以表明直至我的帳戶驅動器?我應該在哪裏添加我的用戶名和密碼才能與我的驅動器相關聯?我應該怎麼做文件才能公開?
非常感謝你爲你的全部注意力。我希望他們能幫助我。