mirror of https://github.com/Xhofe/alist
feat(dropbox): add root_namespace_id to access teams folder (#5929)
* feat(dropbox): add root_namespace_id to access teams folder * fix(dropbox): get_current_account API request * feat(dropbox): extract root_namespace_id properly * style: format codepull/5957/head
parent
9222510d8d
commit
aef952ae68
|
@ -45,7 +45,25 @@ func (d *Dropbox) Init(ctx context.Context) error {
|
|||
if result != query {
|
||||
return fmt.Errorf("failed to check user: %s", string(res))
|
||||
}
|
||||
return nil
|
||||
d.RootNamespaceId, err = d.GetRootNamespaceId(ctx)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *Dropbox) GetRootNamespaceId(ctx context.Context) (string, error) {
|
||||
res, err := d.request("/2/users/get_current_account", http.MethodPost, func(req *resty.Request) {
|
||||
req.SetBody(nil)
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var currentAccountResp CurrentAccountResp
|
||||
err = utils.Json.Unmarshal(res, ¤tAccountResp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
rootNamespaceId := currentAccountResp.RootInfo.RootNamespaceId
|
||||
return rootNamespaceId, nil
|
||||
}
|
||||
|
||||
func (d *Dropbox) Drop(ctx context.Context) error {
|
||||
|
|
|
@ -17,7 +17,8 @@ type Addition struct {
|
|||
ClientID string `json:"client_id" required:"false" help:"Keep it empty if you don't have one"`
|
||||
ClientSecret string `json:"client_secret" required:"false" help:"Keep it empty if you don't have one"`
|
||||
|
||||
AccessToken string
|
||||
AccessToken string
|
||||
RootNamespaceId string
|
||||
}
|
||||
|
||||
var config = driver.Config{
|
||||
|
|
|
@ -23,6 +23,13 @@ type RefreshTokenErrorResp struct {
|
|||
ErrorDescription string `json:"error_description"`
|
||||
}
|
||||
|
||||
type CurrentAccountResp struct {
|
||||
RootInfo struct {
|
||||
RootNamespaceId string `json:"root_namespace_id"`
|
||||
HomeNamespaceId string `json:"home_namespace_id"`
|
||||
} `json:"root_info"`
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Tag string `json:".tag"`
|
||||
Name string `json:"name"`
|
||||
|
|
|
@ -46,12 +46,22 @@ func (d *Dropbox) refreshToken() error {
|
|||
func (d *Dropbox) request(uri, method string, callback base.ReqCallback, retry ...bool) ([]byte, error) {
|
||||
req := base.RestyClient.R()
|
||||
req.SetHeader("Authorization", "Bearer "+d.AccessToken)
|
||||
if method == http.MethodPost {
|
||||
req.SetHeader("Content-Type", "application/json")
|
||||
if d.RootNamespaceId != "" {
|
||||
apiPathRootJson, err := utils.Json.MarshalToString(map[string]interface{}{
|
||||
".tag": "root",
|
||||
"root": d.RootNamespaceId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.SetHeader("Dropbox-API-Path-Root", apiPathRootJson)
|
||||
}
|
||||
if callback != nil {
|
||||
callback(req)
|
||||
}
|
||||
if method == http.MethodPost && req.Body != nil {
|
||||
req.SetHeader("Content-Type", "application/json")
|
||||
}
|
||||
var e ErrorResp
|
||||
req.SetError(&e)
|
||||
res, err := req.Execute(method, d.base+uri)
|
||||
|
|
Loading…
Reference in New Issue