背景

提到rclone这一强大的网盘同步工具,想必很多人会用到其来连接OneDrive,但是有时候的确rclone在同步OneDrive的时候速度不太理想。在参考了 P3TERX 大佬的文章后,才知道原来可能是很多人同时使用rclone自带的API的锅。所以P3TERX 大佬指出可以通过自建OneDrive的API来改善这一问题,还有可能续订Office 365 E5呢。

创建Azure应用

获取Client ID

  • 打开Microsoft Azure 应用注册页面,点击 新注册按钮。

    image.png
    image.png

  • 填写名称,选择受支持的账户类型并填写重定向URL后点击注册按钮

    • 名称随意
    • 受支持的账户类型选择第三个
    • 重定向URL填写 http://localhost
      image.png
      image.png
  • 注册应用后会返回应用首页,复制记好应用程序(客户端)ID 也就是 Client ID。

  • image.png
    image.png

获取Client secret

  • 在左侧点击 证书和密码,再点击中间的 新客户端密码 按钮来创建Client secret。

image.png
image.png

  • 点击添加按钮后就会看到你创建的Client secret,复制保存好。
    image.png
    image.png

设置你创建好的API的权限

  • 点击左侧的 API 权限,点击中间的 Microsoft Graph,接着在右侧的界面中搜索并打勾以下权限,最后点击下方更新权限的按钮

    • Files.Read
    • Files.ReadWrite
    • Files.Read.All
    • Files.ReadWrite.All
    • offline_access
    • User.Read
      image.png
      image.png
  • 添加完成后确认是否有以下权限

    • Files.Read
    • Files.ReadWrite
    • Files.Read.All
    • Files.ReadWrite.All
    • offline_access
    • User.Read

image.png
image.png

使用rclone获取token

你的电脑里已经有rclone的话可以直接看下面(以Windows为例),没有的话请通过这里下载。 - 进入rclone的文件夹,在资源管理器地址栏输入cmd回车或者在文件夹内按住Shift+鼠标右键,选择 在此处打开 Powershell 窗口,就会在当前路径打开命令提示符。 - 在命令提示符或者Powershell内执行以下命令,注意替换"Client_ID" "Client_secret"

rclone authorize "onedrive" "Client_ID" "Client_secret"

-执行以上命令就会弹出浏览器窗口登录账号来授权获取token。显示Success的话可以关掉浏览器回到原来的命令提示符界面,应该可以看到以下信息:

If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth
Log in and authorize rclone for access
Waiting for code...
Got code
Paste the following into your remote machine --->
{"access_token":"xxxxxxxxxxxxxxxxxx","expiry":"2020-02-22T21:18:39.5036298+08:00"}
<---End paste

根据提示复制保存好token

{"access_token":"xxxxxxxxxxxxxxxxxx","expiry":"2020-02-22T21:18:39.5036298+08:00"}

rclone添加OneDrive网盘

  • 继续在上面的命令提示符执行以下命令来添加网盘

    PS C:\Users\Wyane> rclone config
    Current remotes:
    
    Name                 Type
    ====                 ====
    
    e) Edit existing remote
    n) New remote
    d) Delete remote
    r) Rename remote
    c) Copy remote
    s) Set configuration password
    q) Quit config
    e/n/d/r/c/s/q> n  ←这里选择n添加新的网盘
    name> myonedrive
    Type of storage to configure.
    Enter a string value. Press Enter for the default ("").
    Choose a number from below, or type in your own value
    1 / 1Fichier
    \ "fichier"
    2 / Alias for an existing remote
    \ "alias"
    3 / Amazon Drive
    \ "amazon cloud drive"
    4 / Amazon S3 Compliant Storage Provider (AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, etc)
    \ "s3"
    5 / Backblaze B2
    \ "b2"
    6 / Box
    \ "box"
    7 / Cache a remote
    \ "cache"
    8 / Citrix Sharefile
    \ "sharefile"
    9 / Dropbox
    \ "dropbox"
    10 / Encrypt/Decrypt a remote
    \ "crypt"
    11 / FTP Connection
    \ "ftp"
    12 / Google Cloud Storage (this is not Google Drive)
    \ "google cloud storage"
    13 / Google Drive
    \ "drive"
    14 / Google Photos
    \ "google photos"
    15 / Hubic
    \ "hubic"
    16 / In memory object storage system.
    \ "memory"
    17 / JottaCloud
    \ "jottacloud"
    18 / Koofr
    \ "koofr"
    19 / Local Disk
    \ "local"
    20 / Mail.ru Cloud
    \ "mailru"
    21 / Mega
    \ "mega"
    22 / Microsoft Azure Blob Storage
    \ "azureblob"
    23 / Microsoft OneDrive
    \ "onedrive"
    24 / OpenDrive
    \ "opendrive"
    25 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
    \ "swift"
    26 / Pcloud
    \ "pcloud"
    27 / Put.io
    \ "putio"
    28 / QingCloud Object Storage
    \ "qingstor"
    29 / SSH/SFTP Connection
    \ "sftp"
    30 / Sugarsync
    \ "sugarsync"
    31 / Transparently chunk/split large files
    \ "chunker"
    32 / Union merges the contents of several remotes
    \ "union"
    33 / Webdav
    \ "webdav"
    34 / Yandex Disk
    \ "yandex"
    35 / http Connection
    \ "http"
    36 / premiumize.me
    \ "premiumizeme"
    Storage> 23  ←选择OneDrive
    ** See help for onedrive backend at: https://rclone.org/onedrive/ **
    
    Microsoft App Client Id
    Leave blank normally.
    Enter a string value. Press Enter for the default ("").
    client_id> client-id  ←输入前面保存的client id
    Microsoft App Client Secret
    Leave blank normally.
    Enter a string value. Press Enter for the default ("").
    client_secret> client-secret  ←输入前面保存的client secret
    Edit advanced config? (y/n)
    y) Yes
    n) No (default)
    y/n> n  ←输入n选择不用
    Remote config
    Make sure your Redirect URL is set to "http://localhost:53682/" in your custom config.
    Use auto config?
    * Say Y if not sure
    * Say N if you are working on a remote or headless machine
    y) Yes (default)
    n) No
    y/n> n  ←输入n选择不用
    For this to work, you will need rclone available on a machine that has a web browser available.
    Execute the following on your machine (same rclone version recommended) :
    rclone authorize "onedrive" "client_id" "client_secret"
    Then paste the result below:
    result> {"access_token":"XXXXXXXXX","expiry":"2020-02-22T21:18:39.5036298+08:00"}   # 输入 token
    Choose a number from below, or type in an existing value
    1 / OneDrive Personal or Business
    \ "onedrive"
    2 / Root Sharepoint site
    \ "sharepoint"
    3 / Type in driveID
    \ "driveid"
    4 / Type in SiteID
    \ "siteid"
    5 / Search a Sharepoint site
    \ "search"
    Your choice> 1 ←这里选择1
    Found 1 drives, please select the one you want to use:
    0: OneDrive (business) id=xxxxxxxxxxxxxx
    Chose drive to use:> 0 ←输入检测到的网盘编号0
    Found drive 'root' of type 'business', URL: https://wyane-my.sharepoint.com/personal/xxxxxx/Documents
    Is that okay?
    y) Yes
    n) No
    y/n> y ←确认没问题选择 y
    
    --------------------
    [od-e5-api]
    type = onedrive
    client_id = xxxxxxxxxx
    client_secret = xxxxxxxxxxxxxxxx
    token = {"access_token":"xxxxxxxxxxxxxxxxxx","expiry":"2020-02-22T21:18:39.5036298+08:00"}
    drive_id = xxxxxxxxxxxxxxxxxxxxxxxxx
    drive_type = business
    --------------------
    y) Yes this is OK
    e) Edit this remote
    d) Delete this remote
    y/e/d> y # 最后会显示网盘的配置信息并询问你是否正确,如果没问题则输入 y
    Current remotes:
    
    Name                 Type
    ====                 ====
    myonedrive            onedrive
    
    e) Edit existing remote
    n) New remote
    d) Delete remote
    r) Rename remote
    c) Copy remote
    s) Set configuration password
    q) Quit config
    e/n/d/r/c/s/q> q # 输入 q 退出
    

最后可以验证下是否能用自建API来连接。

rclone mkdir myonedrive:/test 

执行成功的话应该可以在OneDrive网盘里面看到test文件夹。

参考资料