stacktic
/
dropbox
Archived
1
0
Fork 0

Update to support new oauth2 API (Fixes #11)

This commit is contained in:
Arnaud Ysmal 2014-12-17 19:32:12 +01:00
parent 2d085bf608
commit 5bb11ef380
2 changed files with 24 additions and 48 deletions

View File

@ -183,20 +183,10 @@ type Dropbox struct {
APIURL string // Normal API URL.
APIContentURL string // URL for transferring files.
APINotifyURL string // URL for realtime notification.
config *oauth2.Options
config *oauth2.Config
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{
@ -212,22 +202,14 @@ func NewDropbox() *Dropbox {
// 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) error {
var err error
var authURL, tokenURL *url.URL
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{
db.config = &oauth2.Config{
ClientID: clientid,
ClientSecret: clientsecret,
AuthURL: authURL,
TokenURL: tokenURL,
TokenStore: db,
Endpoint: oauth2.Endpoint{
AuthURL: "https://www.dropbox.com/1/oauth2/authorize",
TokenURL: "https://api.dropbox.com/1/oauth2/token",
},
}
return nil
}
@ -243,28 +225,22 @@ func (db *Dropbox) AccessToken() string {
}
func (db *Dropbox) client() *http.Client {
var t *oauth2.Transport
var err error
if t, err = db.config.NewTransportFromTokenStore(db); err != nil {
return nil
}
return &http.Client{Transport: t}
return db.config.Client(oauth2.NoContext, db.token)
}
// 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 t *oauth2.Token
var err error
fmt.Printf("Please visit:\n%s\nEnter the code: ",
db.config.AuthCodeURL("", "", ""))
db.config.AuthCodeURL(""))
fmt.Scanln(&code)
if t, err = db.config.NewTransportFromCode(code); err != nil {
if t, err = db.config.Exchange(oauth2.NoContext, code); err != nil {
return err
}
db.token = t.Token()
db.token = t
db.token.TokenType = "Bearer"
return nil
}

View File

@ -132,7 +132,7 @@ func TestAccountInfo(t *testing.T) {
t.Fatalf("could not run test marshalling issue")
}
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: FakeHTTP{
t: t,
Method: "GET",
@ -181,7 +181,7 @@ func TestCopy(t *testing.T) {
ResponseData: js,
}
db = newDropbox(t)
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: fake,
}
@ -215,7 +215,7 @@ func TestCopyRef(t *testing.T) {
t.Fatalf("could not run test due to marshalling issue")
}
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: FakeHTTP{
Method: "GET",
Host: "api.dropbox.com",
@ -247,7 +247,7 @@ func TestCreateFolder(t *testing.T) {
}
db = newDropbox(t)
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: FakeHTTP{
Method: "POST",
Host: "api.dropbox.com",
@ -284,7 +284,7 @@ func TestDelete(t *testing.T) {
}
db = newDropbox(t)
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: FakeHTTP{
t: t,
Method: "POST",
@ -339,7 +339,7 @@ func TestFilesPut(t *testing.T) {
}
db = newDropbox(t)
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: fake,
}
@ -387,7 +387,7 @@ func TestMedia(t *testing.T) {
t.Fatalf("could not run test due to marshalling issue: %s", err)
}
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: FakeHTTP{
Method: "POST",
Host: "api.dropbox.com",
@ -433,7 +433,7 @@ func TestMetadata(t *testing.T) {
ResponseData: js,
}
db = newDropbox(t)
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: fake,
}
@ -510,7 +510,7 @@ func TestMove(t *testing.T) {
}
db = newDropbox(t)
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: FakeHTTP{
t: t,
Method: "POST",
@ -547,7 +547,7 @@ func TestRestore(t *testing.T) {
}
db = newDropbox(t)
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: FakeHTTP{
t: t,
Method: "POST",
@ -594,7 +594,7 @@ func TestRevisions(t *testing.T) {
ResponseData: js,
}
db = newDropbox(t)
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: fake,
}
@ -649,7 +649,7 @@ func TestSearch(t *testing.T) {
},
ResponseData: js,
}
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: fake,
}
@ -702,7 +702,7 @@ func TestShares(t *testing.T) {
t: t,
ResponseData: js,
}
db.config.Client = &http.Client{
http.DefaultClient = &http.Client{
Transport: fake,
}