diff --git a/README.md b/README.md index 65b5700..11b841b 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,9 @@ To register a new client application, please visit https://www.dropbox.com/devel Installation ------------ -This library depends on a fork of the goauth2 library, it can be installed with the go get command: +This library depends on the oauth2 package, it can be installed with the go get command: - $ go get code.google.com/p/stacktic-goauth2 + $ go get github.com/golang/oauth2 This package can be installed with the go get command: diff --git a/dropbox.go b/dropbox.go index 759a5b9..036ec9c 100644 --- a/dropbox.go +++ b/dropbox.go @@ -39,7 +39,7 @@ import ( "strings" "time" - "code.google.com/p/stacktic-goauth2/oauth" + "github.com/golang/oauth2" ) // ErrNotAuth is the error returned when the OAuth token is not provided @@ -178,57 +178,75 @@ type Link struct { // Dropbox client. type Dropbox struct { - RootDirectory string // dropbox or sandbox. - Locale string // Locale sent to the API to translate/format messages. - APIURL string // Normal API URL. - APIContentURL string // URL for transferring files. - APINotifyURL string // URL for realtime notification. - Session oauth.Transport // OAuth 2.0 session. + RootDirectory string // dropbox or sandbox. + Locale string // Locale sent to the API to translate/format messages. + APIURL string // Normal API URL. + APIContentURL string // URL for transferring files. + APINotifyURL string // URL for realtime notification. + config *oauth2.Config + token *oauth2.Token } // NewDropbox returns a new Dropbox configured. func NewDropbox() *Dropbox { - return &Dropbox{ + db := &Dropbox{ RootDirectory: "dropbox", // dropbox or sandbox. Locale: "en", APIURL: "https://api.dropbox.com/1", APIContentURL: "https://api-content.dropbox.com/1", APINotifyURL: "https://api-notify.dropbox.com/1", - Session: oauth.Transport{ - Config: &oauth.Config{ - AuthURL: "https://www.dropbox.com/1/oauth2/authorize", - TokenURL: "https://api.dropbox.com/1/oauth2/token", - }, - }, } + return db } // SetAppInfo sets the clientid (app_key) and clientsecret (app_secret). // You have to register an application on https://www.dropbox.com/developers/apps. -func (db *Dropbox) SetAppInfo(clientid, clientsecret string) { - db.Session.Config.ClientId = clientid - db.Session.Config.ClientSecret = clientsecret +func (db *Dropbox) SetAppInfo(clientid, clientsecret string) error { + var err error + + db.config, err = oauth2.NewConfig( + &oauth2.Options{ + ClientID: clientid, + ClientSecret: clientsecret, + }, + "https://www.dropbox.com/1/oauth2/authorize", + "https://api.dropbox.com/1/oauth2/token") + return err } // SetAccessToken sets access token to avoid calling Auth method. func (db *Dropbox) SetAccessToken(accesstoken string) { - db.Session.Token = &oauth.Token{AccessToken: accesstoken} + db.token = &oauth2.Token{AccessToken: accesstoken} } // AccessToken returns the OAuth access token. func (db *Dropbox) AccessToken() string { - return db.Session.Token.AccessToken + return db.token.AccessToken +} + +func (db *Dropbox) client() *http.Client { + var t oauth2.Transport + + t = db.config.NewTransport() + t.SetToken(db.token) + return &http.Client{Transport: t} } // Auth displays the URL to authorize this application to connect to your account. func (db *Dropbox) Auth() error { var code string + var t oauth2.Transport + var err error fmt.Printf("Please visit:\n%s\nEnter the code: ", - db.Session.Config.AuthCodeURL("")) + db.config.AuthCodeURL("")) fmt.Scanln(&code) - _, err := db.Session.Exchange(code) - return err + if t, err = db.config.NewTransportWithCode(code); err != nil { + return err + } + db.token = t.Token() + db.token.TokenType = "Bearer" + return nil } // Error - all errors generated by HTTP transactions are of this type. @@ -314,7 +332,7 @@ func (db *Dropbox) CommitChunkedUpload(uploadid, dst string, overwrite bool, par } rawurl = fmt.Sprintf("%s/commit_chunked_upload/%s/%s?%s", db.APIContentURL, db.RootDirectory, urlEncode(dst), params.Encode()) - if response, err = db.Session.Client().Post(rawurl, "", nil); err != nil { + if response, err = db.client().Post(rawurl, "", nil); err != nil { return nil, err } defer response.Body.Close() @@ -347,7 +365,7 @@ func (db *Dropbox) ChunkedUpload(session *ChunkUploadResponse, input io.ReadClos } r = &io.LimitedReader{R: input, N: int64(chunksize)} - if response, err = db.Session.Client().Post(rawurl, "application/octet-stream", r); err != nil { + if response, err = db.client().Post(rawurl, "application/octet-stream", r); err != nil { return nil, err } defer response.Body.Close() @@ -401,7 +419,7 @@ func (db *Dropbox) FilesPut(input io.ReadCloser, size int64, dst string, overwri return nil, err } request.Header.Set("Content-Length", strconv.FormatInt(size, 10)) - if response, err = db.Session.Client().Do(request); err != nil { + if response, err = db.client().Do(request); err != nil { return nil, err } defer response.Body.Close() @@ -459,7 +477,7 @@ func (db *Dropbox) Thumbnails(src, format, size string) (io.ReadCloser, int64, * src = src[1:] } rawurl = fmt.Sprintf("%s/thumbnails/%s/%s?format=%s&size=%s", db.APIContentURL, db.RootDirectory, urlEncode(src), urlEncode(format), urlEncode(size)) - if response, err = db.Session.Client().Get(rawurl); err != nil { + if response, err = db.client().Get(rawurl); err != nil { return nil, 0, nil, err } if response.StatusCode == http.StatusOK { @@ -524,7 +542,7 @@ func (db *Dropbox) Download(src, rev string, offset int) (io.ReadCloser, int64, request.Header.Set("Range", fmt.Sprintf("bytes=%d-", offset)) } - if response, err = db.Session.Client().Do(request); err != nil { + if response, err = db.client().Do(request); err != nil { return nil, 0, err } if response.StatusCode == http.StatusOK { @@ -603,7 +621,7 @@ func (db *Dropbox) doRequest(method, path string, params *url.Values, receiver i if request, err = http.NewRequest(method, rawurl, nil); err != nil { return err } - if response, err = db.Session.Client().Do(request); err != nil { + if response, err = db.client().Do(request); err != nil { return err } defer response.Body.Close() diff --git a/dropbox_test.go b/dropbox_test.go index c290c9b..e0c2f43 100644 --- a/dropbox_test.go +++ b/dropbox_test.go @@ -132,7 +132,7 @@ func TestAccountInfo(t *testing.T) { t.Fatalf("could not run test marshalling issue") } - db.Session.Transport = FakeHTTP{t: t, Method: "GET", Host: "api.dropbox.com", Path: "/1/account/info", + http.DefaultTransport = FakeHTTP{t: t, Method: "GET", Host: "api.dropbox.com", Path: "/1/account/info", Params: map[string]string{"locale": "en"}, ResponseData: js} @@ -165,7 +165,7 @@ func TestCopy(t *testing.T) { ResponseData: js} db = newDropbox(t) - db.Session.Transport = fake + http.DefaultTransport = 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 +174,7 @@ func TestCopy(t *testing.T) { delete(fake.Params, "from_path") fake.Params["from_copy_ref"] = from - db.Session.Transport = fake + http.DefaultTransport = fake if received, err = db.Copy(from, to, true); err != nil { t.Errorf("API error: %s", err) } else if !reflect.DeepEqual(expected, *received) { @@ -197,7 +197,7 @@ func TestCopyRef(t *testing.T) { t.Fatalf("could not run test due to marshalling issue") } - db.Session.Transport = FakeHTTP{Method: "GET", Host: "api.dropbox.com", Path: "/1/copy_ref/dropbox/" + filename, t: t, + http.DefaultTransport = 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) @@ -221,7 +221,7 @@ func TestCreateFolder(t *testing.T) { } db = newDropbox(t) - db.Session.Transport = FakeHTTP{Method: "POST", Host: "api.dropbox.com", Path: "/1/fileops/create_folder", + http.DefaultTransport = 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) @@ -246,7 +246,7 @@ func TestDelete(t *testing.T) { } db = newDropbox(t) - db.Session.Transport = FakeHTTP{t: t, Method: "POST", Host: "api.dropbox.com", Path: "/1/fileops/delete", + http.DefaultTransport = 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 { @@ -282,7 +282,7 @@ func TestFilesPut(t *testing.T) { db = newDropbox(t) - db.Session.Transport = fake + http.DefaultTransport = 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 +291,7 @@ func TestFilesPut(t *testing.T) { } fake.Params["parent_rev"] = "12345" - db.Session.Transport = fake + http.DefaultTransport = 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 +300,7 @@ func TestFilesPut(t *testing.T) { } fake.Params["overwrite"] = "true" - db.Session.Transport = fake + http.DefaultTransport = 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,7 +329,7 @@ func TestMedia(t *testing.T) { t.Fatalf("could not run test due to marshalling issue: %s", err) } - db.Session.Transport = FakeHTTP{Method: "POST", Host: "api.dropbox.com", Path: "/1/media/dropbox/" + filename, + http.DefaultTransport = 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) @@ -359,7 +359,7 @@ func TestMetadata(t *testing.T) { db = newDropbox(t) - db.Session.Transport = fake + http.DefaultTransport = 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 +367,7 @@ func TestMetadata(t *testing.T) { } fake.Params["list"] = "true" - db.Session.Transport = fake + http.DefaultTransport = 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 +375,7 @@ func TestMetadata(t *testing.T) { } fake.Params["include_deleted"] = "true" - db.Session.Transport = fake + http.DefaultTransport = 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 +383,7 @@ func TestMetadata(t *testing.T) { } fake.Params["file_limit"] = "20" - db.Session.Transport = fake + http.DefaultTransport = 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 +391,7 @@ func TestMetadata(t *testing.T) { } fake.Params["rev"] = "12345" - db.Session.Transport = fake + http.DefaultTransport = 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 +399,7 @@ func TestMetadata(t *testing.T) { } fake.Params["hash"] = "6789" - db.Session.Transport = fake + http.DefaultTransport = 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 +407,7 @@ func TestMetadata(t *testing.T) { } fake.Params["file_limit"] = "10000" - db.Session.Transport = fake + http.DefaultTransport = 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 +415,7 @@ func TestMetadata(t *testing.T) { } fake.Params["file_limit"] = strconv.FormatInt(int64(MetadataLimitMax), 10) - db.Session.Transport = fake + http.DefaultTransport = 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,7 +440,7 @@ func TestMove(t *testing.T) { } db = newDropbox(t) - db.Session.Transport = FakeHTTP{t: t, Method: "POST", Host: "api.dropbox.com", Path: "/1/fileops/move", + http.DefaultTransport = 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 { @@ -465,7 +465,7 @@ func TestRestore(t *testing.T) { } db = newDropbox(t) - db.Session.Transport = FakeHTTP{t: t, Method: "POST", Host: "api.dropbox.com", Path: "/1/restore/dropbox/" + path, + http.DefaultTransport = 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 { @@ -496,7 +496,7 @@ func TestRevisions(t *testing.T) { db = newDropbox(t) - db.Session.Transport = fake + http.DefaultTransport = fake if received, err = db.Revisions(path, 10); err != nil { t.Errorf("API error: %s", err) } else if !reflect.DeepEqual(expected, received) { @@ -538,7 +538,7 @@ func TestSearch(t *testing.T) { 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.Session.Transport = fake + http.DefaultTransport = 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 +546,7 @@ func TestSearch(t *testing.T) { } fake.Params["include_deleted"] = "true" - db.Session.Transport = fake + http.DefaultTransport = 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 +554,7 @@ func TestSearch(t *testing.T) { } fake.Params["file_limit"] = strconv.FormatInt(int64(SearchLimitDefault), 10) - db.Session.Transport = fake + http.DefaultTransport = 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) { @@ -585,7 +585,7 @@ func TestShares(t *testing.T) { fake := FakeHTTP{Method: "POST", Host: "api.dropbox.com", Path: "/1/shares/dropbox/" + filename, Params: map[string]string{"locale": "en"}, t: t, ResponseData: js} - db.Session.Transport = fake + http.DefaultTransport = fake if received, err = db.Shares(filename, false); err != nil { t.Errorf("API error: %s", err) } else if !reflect.DeepEqual(expected, *received) { @@ -593,7 +593,7 @@ func TestShares(t *testing.T) { } fake.Params["short_url"] = "true" - db.Session.Transport = fake + http.DefaultTransport = fake if received, err = db.Shares(filename, true); err != nil { t.Errorf("API error: %s", err) } else if !reflect.DeepEqual(expected, *received) {