Update to support new oauth2 API
This commit is contained in:
parent
a07cde5996
commit
c30fa0c6e4
|
@ -13,7 +13,7 @@ Installation
|
|||
------------
|
||||
This library depends on the oauth2 package, it can be installed with the go get command:
|
||||
|
||||
$ go get github.com/golang/oauth2
|
||||
$ go get golang.org/x/oauth2
|
||||
|
||||
This package can be installed with the go get command:
|
||||
|
||||
|
|
42
dropbox.go
42
dropbox.go
|
@ -39,7 +39,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/stacktic/oauth2"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
// ErrNotAuth is the error returned when the OAuth token is not provided
|
||||
|
@ -183,10 +183,20 @@ type Dropbox struct {
|
|||
APIURL string // Normal API URL.
|
||||
APIContentURL string // URL for transferring files.
|
||||
APINotifyURL string // URL for realtime notification.
|
||||
config *oauth2.Config
|
||||
config *oauth2.Options
|
||||
token *oauth2.Token
|
||||
}
|
||||
|
||||
// ReadToken returns an existing token (oauth2.TokenStore interface)
|
||||
func (db *Dropbox) ReadToken() (*oauth2.Token, error) {
|
||||
return db.token, nil
|
||||
}
|
||||
|
||||
// WriteToken updates the token (oauth2.TokenStore interface)
|
||||
func (db *Dropbox) WriteToken(t *oauth2.Token) {
|
||||
db.token = t
|
||||
}
|
||||
|
||||
// NewDropbox returns a new Dropbox configured.
|
||||
func NewDropbox() *Dropbox {
|
||||
db := &Dropbox{
|
||||
|
@ -203,15 +213,23 @@ func NewDropbox() *Dropbox {
|
|||
// You have to register an application on https://www.dropbox.com/developers/apps.
|
||||
func (db *Dropbox) SetAppInfo(clientid, clientsecret string) error {
|
||||
var err error
|
||||
var authURL, tokenURL *url.URL
|
||||
|
||||
db.config, err = oauth2.NewConfig(
|
||||
&oauth2.Options{
|
||||
if authURL, err = url.Parse("https://www.dropbox.com/1/oauth2/authorize"); err != nil {
|
||||
return err
|
||||
}
|
||||
if tokenURL, err = url.Parse("https://api.dropbox.com/1/oauth2/token"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
db.config = &oauth2.Options{
|
||||
ClientID: clientid,
|
||||
ClientSecret: clientsecret,
|
||||
},
|
||||
"https://www.dropbox.com/1/oauth2/authorize",
|
||||
"https://api.dropbox.com/1/oauth2/token")
|
||||
return err
|
||||
AuthURL: authURL,
|
||||
TokenURL: tokenURL,
|
||||
TokenStore: db,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetAccessToken sets access token to avoid calling Auth method.
|
||||
|
@ -226,9 +244,11 @@ func (db *Dropbox) AccessToken() string {
|
|||
|
||||
func (db *Dropbox) client() *http.Client {
|
||||
var t *oauth2.Transport
|
||||
var err error
|
||||
|
||||
t = db.config.NewTransport()
|
||||
t.SetToken(db.token)
|
||||
if t, err = db.config.NewTransportFromTokenStore(db); err != nil {
|
||||
return nil
|
||||
}
|
||||
return &http.Client{Transport: t}
|
||||
}
|
||||
|
||||
|
@ -241,7 +261,7 @@ func (db *Dropbox) Auth() error {
|
|||
fmt.Printf("Please visit:\n%s\nEnter the code: ",
|
||||
db.config.AuthCodeURL("", "", ""))
|
||||
fmt.Scanln(&code)
|
||||
if t, err = db.config.NewTransportWithCode(code); err != nil {
|
||||
if t, err = db.config.NewTransportFromCode(code); err != nil {
|
||||
return err
|
||||
}
|
||||
db.token = t.Token()
|
||||
|
|
229
dropbox_test.go
229
dropbox_test.go
|
@ -132,9 +132,16 @@ func TestAccountInfo(t *testing.T) {
|
|||
t.Fatalf("could not run test marshalling issue")
|
||||
}
|
||||
|
||||
db.config.Transport = FakeHTTP{t: t, Method: "GET", Host: "api.dropbox.com", Path: "/1/account/info",
|
||||
db.config.Client = &http.Client{
|
||||
Transport: FakeHTTP{
|
||||
t: t,
|
||||
Method: "GET",
|
||||
Host: "api.dropbox.com",
|
||||
Path: "/1/account/info",
|
||||
Params: map[string]string{"locale": "en"},
|
||||
ResponseData: js}
|
||||
ResponseData: js,
|
||||
},
|
||||
}
|
||||
|
||||
if received, err = db.GetAccountInfo(); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
|
@ -160,12 +167,24 @@ func TestCopy(t *testing.T) {
|
|||
t.Fatalf("could not run test marshalling issue")
|
||||
}
|
||||
|
||||
fake = FakeHTTP{t: t, Method: "POST", Host: "api.dropbox.com", Path: "/1/fileops/copy",
|
||||
Params: map[string]string{"root": "dropbox", "from_path": from, "to_path": to, "locale": "en"},
|
||||
ResponseData: js}
|
||||
|
||||
fake = FakeHTTP{
|
||||
t: t,
|
||||
Method: "POST",
|
||||
Host: "api.dropbox.com",
|
||||
Path: "/1/fileops/copy",
|
||||
Params: map[string]string{
|
||||
"root": "dropbox",
|
||||
"from_path": from,
|
||||
"to_path": to,
|
||||
"locale": "en",
|
||||
},
|
||||
ResponseData: js,
|
||||
}
|
||||
db = newDropbox(t)
|
||||
db.config.Transport = fake
|
||||
db.config.Client = &http.Client{
|
||||
Transport: fake,
|
||||
}
|
||||
|
||||
if received, err = db.Copy(from, to, false); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -174,7 +193,6 @@ func TestCopy(t *testing.T) {
|
|||
|
||||
delete(fake.Params, "from_path")
|
||||
fake.Params["from_copy_ref"] = from
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Copy(from, to, true); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -197,8 +215,16 @@ func TestCopyRef(t *testing.T) {
|
|||
t.Fatalf("could not run test due to marshalling issue")
|
||||
}
|
||||
|
||||
db.config.Transport = FakeHTTP{Method: "GET", Host: "api.dropbox.com", Path: "/1/copy_ref/dropbox/" + filename, t: t,
|
||||
Params: map[string]string{"locale": "en"}, ResponseData: js}
|
||||
db.config.Client = &http.Client{
|
||||
Transport: FakeHTTP{
|
||||
Method: "GET",
|
||||
Host: "api.dropbox.com",
|
||||
Path: "/1/copy_ref/dropbox/" + filename,
|
||||
t: t,
|
||||
Params: map[string]string{"locale": "en"},
|
||||
ResponseData: js,
|
||||
},
|
||||
}
|
||||
if received, err = db.CopyRef(filename); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -221,8 +247,20 @@ func TestCreateFolder(t *testing.T) {
|
|||
}
|
||||
|
||||
db = newDropbox(t)
|
||||
db.config.Transport = FakeHTTP{Method: "POST", Host: "api.dropbox.com", Path: "/1/fileops/create_folder",
|
||||
Params: map[string]string{"root": "dropbox", "path": foldername, "locale": "en"}, t: t, ResponseData: js}
|
||||
db.config.Client = &http.Client{
|
||||
Transport: FakeHTTP{
|
||||
Method: "POST",
|
||||
Host: "api.dropbox.com",
|
||||
Path: "/1/fileops/create_folder",
|
||||
Params: map[string]string{
|
||||
"root": "dropbox",
|
||||
"path": foldername,
|
||||
"locale": "en",
|
||||
},
|
||||
t: t,
|
||||
ResponseData: js,
|
||||
},
|
||||
}
|
||||
if received, err = db.CreateFolder(foldername); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -246,9 +284,20 @@ func TestDelete(t *testing.T) {
|
|||
}
|
||||
|
||||
db = newDropbox(t)
|
||||
db.config.Transport = FakeHTTP{t: t, Method: "POST", Host: "api.dropbox.com", Path: "/1/fileops/delete",
|
||||
Params: map[string]string{"root": "dropbox", "path": path, "locale": "en"},
|
||||
ResponseData: js}
|
||||
db.config.Client = &http.Client{
|
||||
Transport: FakeHTTP{
|
||||
t: t,
|
||||
Method: "POST",
|
||||
Host: "api.dropbox.com",
|
||||
Path: "/1/fileops/delete",
|
||||
Params: map[string]string{
|
||||
"root": "dropbox",
|
||||
"path": path,
|
||||
"locale": "en",
|
||||
},
|
||||
ResponseData: js,
|
||||
},
|
||||
}
|
||||
if received, err = db.Delete(path); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -276,13 +325,24 @@ func TestFilesPut(t *testing.T) {
|
|||
t.Fatalf("could not run test marshalling issue")
|
||||
}
|
||||
|
||||
fake = FakeHTTP{t: t, Method: "PUT", Host: "api-content.dropbox.com", Path: "/1/files_put/dropbox/" + filename,
|
||||
Params: map[string]string{"locale": "en", "overwrite": "false"},
|
||||
ResponseData: js, RequestData: content}
|
||||
fake = FakeHTTP{
|
||||
t: t,
|
||||
Method: "PUT",
|
||||
Host: "api-content.dropbox.com",
|
||||
Path: "/1/files_put/dropbox/" + filename,
|
||||
Params: map[string]string{
|
||||
"locale": "en",
|
||||
"overwrite": "false",
|
||||
},
|
||||
ResponseData: js,
|
||||
RequestData: content,
|
||||
}
|
||||
|
||||
db = newDropbox(t)
|
||||
db.config.Client = &http.Client{
|
||||
Transport: fake,
|
||||
}
|
||||
|
||||
db.config.Transport = fake
|
||||
received, err = db.FilesPut(ioutil.NopCloser(bytes.NewBuffer(content)), int64(len(content)), filename, false, "")
|
||||
if err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
|
@ -291,7 +351,6 @@ func TestFilesPut(t *testing.T) {
|
|||
}
|
||||
|
||||
fake.Params["parent_rev"] = "12345"
|
||||
db.config.Transport = fake
|
||||
received, err = db.FilesPut(ioutil.NopCloser(bytes.NewBuffer(content)), int64(len(content)), filename, false, "12345")
|
||||
if err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
|
@ -300,7 +359,6 @@ func TestFilesPut(t *testing.T) {
|
|||
}
|
||||
|
||||
fake.Params["overwrite"] = "true"
|
||||
db.config.Transport = fake
|
||||
received, err = db.FilesPut(ioutil.NopCloser(bytes.NewBuffer(content)), int64(len(content)), filename, true, "12345")
|
||||
if err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
|
@ -329,8 +387,16 @@ func TestMedia(t *testing.T) {
|
|||
t.Fatalf("could not run test due to marshalling issue: %s", err)
|
||||
}
|
||||
|
||||
db.config.Transport = FakeHTTP{Method: "POST", Host: "api.dropbox.com", Path: "/1/media/dropbox/" + filename,
|
||||
Params: map[string]string{"locale": "en"}, t: t, ResponseData: js}
|
||||
db.config.Client = &http.Client{
|
||||
Transport: FakeHTTP{
|
||||
Method: "POST",
|
||||
Host: "api.dropbox.com",
|
||||
Path: "/1/media/dropbox/" + filename,
|
||||
Params: map[string]string{"locale": "en"},
|
||||
t: t,
|
||||
ResponseData: js,
|
||||
},
|
||||
}
|
||||
if received, err = db.Media(filename); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -353,13 +419,24 @@ func TestMetadata(t *testing.T) {
|
|||
t.Fatalf("could not run test marshalling issue")
|
||||
}
|
||||
|
||||
fake = FakeHTTP{t: t, Method: "GET", Host: "api.dropbox.com", Path: "/1/metadata/dropbox/" + path,
|
||||
Params: map[string]string{"list": "false", "include_deleted": "false", "file_limit": "10", "locale": "en"},
|
||||
ResponseData: js}
|
||||
|
||||
fake = FakeHTTP{
|
||||
t: t,
|
||||
Method: "GET",
|
||||
Host: "api.dropbox.com",
|
||||
Path: "/1/metadata/dropbox/" + path,
|
||||
Params: map[string]string{
|
||||
"list": "false",
|
||||
"include_deleted": "false",
|
||||
"file_limit": "10",
|
||||
"locale": "en",
|
||||
},
|
||||
ResponseData: js,
|
||||
}
|
||||
db = newDropbox(t)
|
||||
db.config.Client = &http.Client{
|
||||
Transport: fake,
|
||||
}
|
||||
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Metadata(path, false, false, "", "", 10); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -367,7 +444,6 @@ func TestMetadata(t *testing.T) {
|
|||
}
|
||||
|
||||
fake.Params["list"] = "true"
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Metadata(path, true, false, "", "", 10); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -375,7 +451,6 @@ func TestMetadata(t *testing.T) {
|
|||
}
|
||||
|
||||
fake.Params["include_deleted"] = "true"
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Metadata(path, true, true, "", "", 10); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -383,7 +458,6 @@ func TestMetadata(t *testing.T) {
|
|||
}
|
||||
|
||||
fake.Params["file_limit"] = "20"
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Metadata(path, true, true, "", "", 20); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -391,7 +465,6 @@ func TestMetadata(t *testing.T) {
|
|||
}
|
||||
|
||||
fake.Params["rev"] = "12345"
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Metadata(path, true, true, "", "12345", 20); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -399,7 +472,6 @@ func TestMetadata(t *testing.T) {
|
|||
}
|
||||
|
||||
fake.Params["hash"] = "6789"
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Metadata(path, true, true, "6789", "12345", 20); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -407,7 +479,6 @@ func TestMetadata(t *testing.T) {
|
|||
}
|
||||
|
||||
fake.Params["file_limit"] = "10000"
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Metadata(path, true, true, "6789", "12345", 0); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -415,7 +486,6 @@ func TestMetadata(t *testing.T) {
|
|||
}
|
||||
|
||||
fake.Params["file_limit"] = strconv.FormatInt(int64(MetadataLimitMax), 10)
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Metadata(path, true, true, "6789", "12345", MetadataLimitMax+1); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -440,9 +510,21 @@ func TestMove(t *testing.T) {
|
|||
}
|
||||
|
||||
db = newDropbox(t)
|
||||
db.config.Transport = FakeHTTP{t: t, Method: "POST", Host: "api.dropbox.com", Path: "/1/fileops/move",
|
||||
Params: map[string]string{"root": "dropbox", "from_path": from, "to_path": to, "locale": "en"},
|
||||
ResponseData: js}
|
||||
db.config.Client = &http.Client{
|
||||
Transport: FakeHTTP{
|
||||
t: t,
|
||||
Method: "POST",
|
||||
Host: "api.dropbox.com",
|
||||
Path: "/1/fileops/move",
|
||||
Params: map[string]string{
|
||||
"root": "dropbox",
|
||||
"from_path": from,
|
||||
"to_path": to,
|
||||
"locale": "en",
|
||||
},
|
||||
ResponseData: js,
|
||||
},
|
||||
}
|
||||
if received, err = db.Move(from, to); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -465,9 +547,19 @@ func TestRestore(t *testing.T) {
|
|||
}
|
||||
|
||||
db = newDropbox(t)
|
||||
db.config.Transport = FakeHTTP{t: t, Method: "POST", Host: "api.dropbox.com", Path: "/1/restore/dropbox/" + path,
|
||||
Params: map[string]string{"rev": expected.Revision, "locale": "en"},
|
||||
ResponseData: js}
|
||||
db.config.Client = &http.Client{
|
||||
Transport: FakeHTTP{
|
||||
t: t,
|
||||
Method: "POST",
|
||||
Host: "api.dropbox.com",
|
||||
Path: "/1/restore/dropbox/" + path,
|
||||
Params: map[string]string{
|
||||
"rev": expected.Revision,
|
||||
"locale": "en",
|
||||
},
|
||||
ResponseData: js,
|
||||
},
|
||||
}
|
||||
if received, err = db.Restore(path, expected.Revision); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -490,13 +582,22 @@ func TestRevisions(t *testing.T) {
|
|||
t.Fatalf("could not run test marshalling issue")
|
||||
}
|
||||
|
||||
fake = FakeHTTP{t: t, Method: "GET", Host: "api.dropbox.com", Path: "/1/revisions/dropbox/" + path,
|
||||
Params: map[string]string{"rev_limit": "10", "locale": "en"},
|
||||
ResponseData: js}
|
||||
|
||||
fake = FakeHTTP{
|
||||
t: t,
|
||||
Method: "GET",
|
||||
Host: "api.dropbox.com",
|
||||
Path: "/1/revisions/dropbox/" + path,
|
||||
Params: map[string]string{
|
||||
"rev_limit": "10",
|
||||
"locale": "en",
|
||||
},
|
||||
ResponseData: js,
|
||||
}
|
||||
db = newDropbox(t)
|
||||
db.config.Client = &http.Client{
|
||||
Transport: fake,
|
||||
}
|
||||
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Revisions(path, 10); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, received) {
|
||||
|
@ -535,10 +636,23 @@ func TestSearch(t *testing.T) {
|
|||
t.Fatalf("could not run test due to marshalling issue")
|
||||
}
|
||||
|
||||
fake := FakeHTTP{Method: "GET", Host: "api.dropbox.com", Path: "/1/search/dropbox/" + dirname, t: t,
|
||||
Params: map[string]string{"locale": "en", "query": "foo bar", "file_limit": "10", "include_deleted": "false"}, ResponseData: js}
|
||||
fake := FakeHTTP{
|
||||
Method: "GET",
|
||||
Host: "api.dropbox.com",
|
||||
Path: "/1/search/dropbox/" + dirname,
|
||||
t: t,
|
||||
Params: map[string]string{
|
||||
"locale": "en",
|
||||
"query": "foo bar",
|
||||
"file_limit": "10",
|
||||
"include_deleted": "false",
|
||||
},
|
||||
ResponseData: js,
|
||||
}
|
||||
db.config.Client = &http.Client{
|
||||
Transport: fake,
|
||||
}
|
||||
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Search(dirname, "foo bar", 10, false); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, received) {
|
||||
|
@ -546,7 +660,6 @@ func TestSearch(t *testing.T) {
|
|||
}
|
||||
|
||||
fake.Params["include_deleted"] = "true"
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Search(dirname, "foo bar", 10, true); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, received) {
|
||||
|
@ -554,7 +667,6 @@ func TestSearch(t *testing.T) {
|
|||
}
|
||||
|
||||
fake.Params["file_limit"] = strconv.FormatInt(int64(SearchLimitDefault), 10)
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Search(dirname, "foo bar", 0, true); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, received) {
|
||||
|
@ -582,10 +694,18 @@ func TestShares(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatalf("could not run test due to marshalling issue")
|
||||
}
|
||||
fake := FakeHTTP{Method: "POST", Host: "api.dropbox.com", Path: "/1/shares/dropbox/" + filename,
|
||||
Params: map[string]string{"locale": "en"}, t: t, ResponseData: js}
|
||||
fake := FakeHTTP{
|
||||
Method: "POST",
|
||||
Host: "api.dropbox.com",
|
||||
Path: "/1/shares/dropbox/" + filename,
|
||||
Params: map[string]string{"locale": "en"},
|
||||
t: t,
|
||||
ResponseData: js,
|
||||
}
|
||||
db.config.Client = &http.Client{
|
||||
Transport: fake,
|
||||
}
|
||||
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Shares(filename, false); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
@ -593,7 +713,6 @@ func TestShares(t *testing.T) {
|
|||
}
|
||||
|
||||
fake.Params["short_url"] = "true"
|
||||
db.config.Transport = fake
|
||||
if received, err = db.Shares(filename, true); err != nil {
|
||||
t.Errorf("API error: %s", err)
|
||||
} else if !reflect.DeepEqual(expected, *received) {
|
||||
|
|
Reference in New Issue
Block a user