github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/rclone_21.patch (about)

     1  diff --git a/backend/onedrive/onedrive.go b/backend/onedrive/onedrive.go
     2  index b64d68263..1d73d8231 100644
     3  --- a/backend/onedrive/onedrive.go
     4  +++ b/backend/onedrive/onedrive.go
     5  @@ -43,7 +43,10 @@ const (
     6   	minSleep                    = 10 * time.Millisecond
     7   	maxSleep                    = 2 * time.Second
     8   	decayConstant               = 2 // bigger for slower decay, exponential
     9  -	graphURL                    = "https://graph.microsoft.com/v1.0"
    10  +	graphAPIEndpoint            = "https://graph.microsoft.com"
    11  +	authEndpoint                = "https://login.microsoftonline.com"
    12  +	graphAPIEndpoint21V         = "https://microsoftgraph.chinacloudapi.cn"
    13  +	authEndpoint21V             = "https://login.chinacloudapi.cn"
    14   	configDriveID               = "drive_id"
    15   	configDriveType             = "drive_type"
    16   	driveTypePersonal           = "personal"
    17  @@ -56,11 +59,15 @@ const (
    18   // Globals
    19   var (
    20   	// Description of how to auth for this app for a business account
    21  +	oauthEndpoint = &oauth2.Endpoint{
    22  +		AuthURL:  authEndpoint + "/common/oauth2/v2.0/authorize",
    23  +		TokenURL: authEndpoint + "/common/oauth2/v2.0/token",
    24  +	}
    25  +	oauthEndpointV21 = &oauth2.Endpoint{
    26  +		AuthURL:  authEndpoint21V + "/common/oauth2/v2.0/authorize",
    27  +		TokenURL: authEndpoint21V + "/common/oauth2/v2.0/token",
    28  +	}
    29   	oauthConfig = &oauth2.Config{
    30  -		Endpoint: oauth2.Endpoint{
    31  -			AuthURL:  "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
    32  -			TokenURL: "https://login.microsoftonline.com/common/oauth2/v2.0/token",
    33  -		},
    34   		Scopes:       []string{"Files.Read", "Files.ReadWrite", "Files.Read.All", "Files.ReadWrite.All", "offline_access"},
    35   		ClientID:     rcloneClientID,
    36   		ClientSecret: obscure.MustReveal(rcloneEncryptedClientSecret),
    37  @@ -79,8 +86,22 @@ func init() {
    38   		Description: "Microsoft OneDrive",
    39   		NewFs:       NewFs,
    40   		Config: func(name string, m configmap.Mapper) {
    41  +			opt := new(Options)
    42  +			err := configstruct.Set(m, opt)
    43  +			if err != nil {
    44  +				fs.Errorf(nil, "Couldn't parse config into struct: %v", err)
    45  +				return
    46  +			}
    47  +
    48  +			graphURL := graphAPIEndpoint + "/v1.0"
    49  +			oauthConfig.Endpoint = *oauthEndpoint
    50  +			if opt.Is21Vianet {
    51  +				graphURL = graphAPIEndpoint21V + "/v1.0"
    52  +				oauthConfig.Endpoint = *oauthEndpointV21
    53  +			}
    54  +
    55   			ctx := context.TODO()
    56  -			err := oauthutil.Config("onedrive", name, m, oauthConfig)
    57  +			err = oauthutil.Config("onedrive", name, m, oauthConfig)
    58   			if err != nil {
    59   				log.Fatalf("Failed to configure token: %v", err)
    60   				return
    61  @@ -223,6 +244,10 @@ func init() {
    62   		}, {
    63   			Name: config.ConfigClientSecret,
    64   			Help: "Microsoft App Client Secret\nLeave blank normally.",
    65  +		}, {
    66  +			Name:    "is_21vianet_version",
    67  +			Default: false,
    68  +			Help:    "OneDrive operated by 21Vianet (世纪互联).",
    69   		}, {
    70   			Name: "chunk_size",
    71   			Help: `Chunk size to upload files with - must be multiple of 320k (327,680 bytes).
    72  @@ -258,6 +283,7 @@ listing, set this option.`,
    73   
    74   // Options defines the configuration for this backend
    75   type Options struct {
    76  +	Is21Vianet         bool          `config:"is_21vianet_version"`
    77   	ChunkSize          fs.SizeSuffix `config:"chunk_size"`
    78   	DriveID            string        `config:"drive_id"`
    79   	DriveType          string        `config:"drive_type"`
    80  @@ -494,6 +520,13 @@ func NewFs(name, root string, m configmap.Mapper) (fs.Fs, error) {
    81   		return nil, errors.New("unable to get drive_id and drive_type - if you are upgrading from older versions of rclone, please run `rclone config` and re-configure this backend")
    82   	}
    83   
    84  +	rootURL := graphAPIEndpoint + "/v1.0" + "/drives/" + opt.DriveID
    85  +	oauthConfig.Endpoint = *oauthEndpoint
    86  +	if opt.Is21Vianet {
    87  +		rootURL = graphAPIEndpoint21V + "/v1.0" + "/me/drive"
    88  +		oauthConfig.Endpoint = *oauthEndpointV21
    89  +	}
    90  +
    91   	root = parsePath(root)
    92   	oAuthClient, ts, err := oauthutil.NewClient(name, m, oauthConfig)
    93   	if err != nil {
    94  @@ -506,7 +539,7 @@ func NewFs(name, root string, m configmap.Mapper) (fs.Fs, error) {
    95   		opt:       *opt,
    96   		driveID:   opt.DriveID,
    97   		driveType: opt.DriveType,
    98  -		srv:       rest.NewClient(oAuthClient).SetRoot(graphURL + "/drives/" + opt.DriveID),
    99  +		srv:       rest.NewClient(oAuthClient).SetRoot(rootURL),
   100   		pacer:     fs.NewPacer(pacer.NewDefault(pacer.MinSleep(minSleep), pacer.MaxSleep(maxSleep), pacer.DecayConstant(decayConstant))),
   101   	}
   102   	f.features = (&fs.Features{
   103  @@ -1705,7 +1738,8 @@ func newOptsCall(normalizedID string, method string, route string) (opts rest.Op
   104   func parseNormalizedID(ID string) (string, string, string) {
   105   	if strings.Index(ID, "#") >= 0 {
   106   		s := strings.Split(ID, "#")
   107  -		return s[1], s[0], graphURL + "/drives"
   108  +		// return s[1], s[0], graphURL + "/drives"
   109  +		return s[1], "", ""
   110   	}
   111   	return ID, "", ""
   112   }
   113