"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
onedrive: add configurable SharePoint tenant API version
Mattias Michaux4e7a21bead98
diff --git a/backend/onedrive/onedrive.go b/backend/onedrive/onedrive.goindex 47bdcbdc5..888435818 100644--- a/backend/onedrive/onedrive.go+++ b/backend/onedrive/onedrive.go@@ -56,6 +56,7 @@ const (driveTypeSharepoint = "documentLibrary"defaultChunkSize = 10 * fs.MebichunkSizeMultiple = 320 * fs.Kibi+ defaultTenantAPIVersion = "v2.0"// maxSinglePartSize is the size at which Graph stops accepting an upload in a// single request (PUT /items/{id}/content). Microsoft documents this as// "250 MB", see@@ -166,7 +167,7 @@ See: https://github.com/rclone/rclone/issues/1716Name: "tenant_url",Help: `The tenant URL for non-admin OneDrive access.-Set this to your SharePoint tenant URL to use the SharePoint v2.0 API+Set this to your SharePoint tenant URL to use the SharePoint APIendpoint instead of the standard Microsoft Graph API. This allowsaccessing business OneDrive without admin consent.@@ -177,6 +178,16 @@ for "driveAccessToken" in the network requests. Look for theExample: https://your-tenant.sharepoint.com/_api`,Default: "",Advanced: true,+ }, {+ Name: "tenant_api_version",+ Help: `The SharePoint API version to use with tenant_url.++Set this to the SharePoint API version matching the browser-extracted+access token. For example, use v2.1 with a driveAccessTokenV21 token.++This only applies when tenant_url is set.`,+ Default: defaultTenantAPIVersion,+ Advanced: true,}, {Name: "chunk_size",Help: `Chunk size to upload files with - must be multiple of 320k (327,680 bytes).@@ -509,12 +520,20 @@ func getRegionURL(m configmap.Mapper) (region, graphURL string) {// Check if tenant_url is provided for non-admin modetenantURL, _ := m.Get("tenant_url")if tenantURL != "" {- graphURL = tenantURL + "/v2.0"+ tenantAPIVersion, _ := m.Get("tenant_api_version")+ graphURL = tenantAPIEndpoint(tenantURL, tenantAPIVersion)}return region, graphURL}+func tenantAPIEndpoint(tenantURL, tenantAPIVersion string) string {+ if tenantAPIVersion == "" {+ tenantAPIVersion = defaultTenantAPIVersion+ }+ return strings.TrimRight(tenantURL, "/") + "/" + strings.TrimLeft(tenantAPIVersion, "/")+}+// Config for chooseDrivetype chooseDriveOpt struct {opts rest.Opts@@ -808,6 +827,7 @@ type Options struct {UploadCutoff fs.SizeSuffix `config:"upload_cutoff"`ChunkSize fs.SizeSuffix `config:"chunk_size"`TenantURL string `config:"tenant_url"`+ TenantAPIVersion string `config:"tenant_api_version"`DriveID string `config:"drive_id"`DriveType string `config:"drive_type"`RootFolderID string `config:"root_folder_id"`@@ -1115,7 +1135,7 @@ func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, erootURL := graphAPIEndpoint[opt.Region] + "/v1.0" + "/drives/" + opt.DriveIDif opt.TenantURL != "" {- rootURL = opt.TenantURL + "/v2.0" + "/drives/" + opt.DriveID+ rootURL = tenantAPIEndpoint(opt.TenantURL, opt.TenantAPIVersion) + "/drives/" + opt.DriveID}oauthConfig, err := makeOauthConfig(ctx, opt)@@ -2918,7 +2938,7 @@ func (o *Object) ID() string {func (f *Fs) parseNormalizedID(ID string) (string, string, string) {var rootURL stringif f.opt.TenantURL != "" {- rootURL = f.opt.TenantURL + "/v2.0/drives"+ rootURL = tenantAPIEndpoint(f.opt.TenantURL, f.opt.TenantAPIVersion) + "/drives"} else {rootURL = graphAPIEndpoint[f.opt.Region] + "/v1.0/drives"}@@ -3119,7 +3139,7 @@ func (f *Fs) changeNotifyNextChange(ctx context.Context, token string) (delta apfunc (f *Fs) buildDriveDeltaOpts(token string) rest.Opts {var rootURL stringif f.opt.TenantURL != "" {- rootURL = f.opt.TenantURL + "/v2.0/drives"+ rootURL = tenantAPIEndpoint(f.opt.TenantURL, f.opt.TenantAPIVersion) + "/drives"} else {rootURL = graphAPIEndpoint[f.opt.Region] + "/v1.0/drives"}diff --git a/backend/onedrive/onedrive_internal_test.go b/backend/onedrive/onedrive_internal_test.goindex 900a63da0..566e6157d 100644--- a/backend/onedrive/onedrive_internal_test.go+++ b/backend/onedrive/onedrive_internal_test.go@@ -42,6 +42,40 @@ func TestMain(m *testing.M) {fstest.TestMain(m)}+func TestTenantAPIEndpoint(t *testing.T) {+ for _, tc := range []struct {+ name string+ tenantURL string+ tenantAPIVersion string+ want string+ }{+ {+ name: "default",+ tenantURL: "https://example-my.sharepoint.com/_api",+ want: "https://example-my.sharepoint.com/_api/v2.0",+ }, {+ name: "explicit v2.1",+ tenantURL: "https://example-my.sharepoint.com/_api",+ tenantAPIVersion: "v2.1",+ want: "https://example-my.sharepoint.com/_api/v2.1",+ }, {+ name: "future version",+ tenantURL: "https://example-my.sharepoint.com/_api",+ tenantAPIVersion: "v2.2",+ want: "https://example-my.sharepoint.com/_api/v2.2",+ }, {+ name: "trailing slash",+ tenantURL: "https://example-my.sharepoint.com/_api/",+ tenantAPIVersion: "/v2.1",+ want: "https://example-my.sharepoint.com/_api/v2.1",+ },+ } {+ t.Run(tc.name, func(t *testing.T) {+ assert.Equal(t, tc.want, tenantAPIEndpoint(tc.tenantURL, tc.tenantAPIVersion))+ })+ }+}+// sharingRefused caches whether the remote refuses sharing invitations.var sharingRefused *booldiff --git a/docs/content/onedrive.md b/docs/content/onedrive.mdindex f440419b6..0d3f68e93 100644--- a/docs/content/onedrive.md+++ b/docs/content/onedrive.md@@ -242,7 +242,7 @@ OneDrive files. Take care to safeguard these credentials.If you do not have admin access to your organization's OneDrive forBusiness, you can still connect by manually providing the SharePointtenant URL and drive ID. This works by overriding the base API URL-from the standard Microsoft Graph endpoint to the SharePoint v2.0+from the standard Microsoft Graph endpoint to the SharePoint APIendpoint.#### Steps to manually obtain credentials@@ -256,6 +256,13 @@ endpoint.".driveAccessToken": "access_token={access_token}"```+ In some guest access scenarios SharePoint may also expose a matching+ v2.1 URL and token pair:+ ```json+ ".driveUrlV21": "{tenant_url}/v2.1/drives/{drive_id}",+ ".driveAccessTokenV21": "access_token={access_token}"+ ```+#### Rclone configurationUse the extracted values to configure your remote:@@ -268,6 +275,14 @@ tenant_url = {tenant_url}drive_type = business```+If you use a v2.1 token, set the matching SharePoint API version:++```ini+tenant_api_version = v2.1+```++If `tenant_api_version` is omitted, rclone defaults to `v2.0`.+Since the exact expiry time cannot be determined from web traffic,set the expiry to a future date. Note that the token will eventuallyexpire and you will need to repeat the process to obtain a new one.