\

OneDrive APIへの接続

OneDrive APIに接続するためには、まずAzureアプリを登録する必要があります。登録した後、OneDriveにログインし、認証コードを取得します。この認証コードを使ってアクセストークンを取得し、そのアクセストークンを使ってMicrosoft Graphによる「OneDriveへのファイルアップロード」や「OneDriveからのファイルダウンロード」を実現します。

PythonでのOneDrive APIの利用

PythonでOneDrive APIを利用するためには、まずonedrivesdkをインストールします。その後、以下のコードを使ってOneDrive APIに接続します。

import onedrivesdk

redirect_uri = 'http://localhost:8080/'
client_secret = 'your_client_secret'
client_id='your_client_id'
api_base_url='https://api.onedrive.com/v1.0/'
scopes= ['wl.signin', 'wl.offline_access', 'onedrive.readwrite']

http_provider = onedrivesdk.HttpProvider()
auth_provider = onedrivesdk.AuthProvider(http_provider=http_provider, client_id=client_id, scopes=scopes)

client = onedrivesdk.OneDriveClient(api_base_url, auth_provider, http_provider)
auth_url = client.auth_provider.get_auth_url(redirect_uri)

print('Paste this URL into your browser, approve the app\'s access.')
print('Copy everything in the address bar after "code=", and paste it below.')
print(auth_url)

code = input('Paste code here: ')
client.auth_provider.authenticate(code, redirect_uri, client_secret)

このコードは、ブラウザとコンソール間でのコピーペーストを必要とします。この手動作業を減らすために、GetAuthCodeServerというヘルパークラスを使用することもできます。ただし、このヘルパークラスはウェブサーバーを起動するため、すべての環境で使用することはできません。

以上がPythonを使ってOneDrive APIに接続する基本的な手順です。これを基に、OneDriveのファイル操作を自動化するアプリケーションを開発することが可能です。

投稿者 admin

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です