Skip to content

Instantly share code, notes, and snippets.

@unakatsuo
Created October 15, 2017 02:43
Show Gist options
  • Save unakatsuo/0dcab7898d092d87a77d684f3e71621b to your computer and use it in GitHub Desktop.
Save unakatsuo/0dcab7898d092d87a77d684f3e71621b to your computer and use it in GitHub Desktop.
Dropbox ListFolderLongPoll example with no HTTP Authorization header
package dropbox
import (
"log"
"net/http"
"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox"
"github.com/dropbox/dropbox-sdk-go-unofficial/dropbox/files"
)
type noauthTransport struct {
http.Transport
}
func (t *noauthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Del("Authorization")
return t.Transport.RoundTrip(req)
}
func newNoAuthClient() *http.Client {
return &http.Client{
Transport: &noauthTransport{},
}
}
func FolderPoll(folderPath string) error {
config := dropbox.Config{
Token: "your secret",
}
dbx := files.New(config)
reqListFolder := files.NewListFolderArg(folderPath)
res, err := dbx.ListFolder(reqListFolder)
if err != nil {
return err
}
cursor := res.Cursor
log.Printf("Start to poll '%s'", folderPath)
for {
noauthdbx := files.New(dropbox.Config{Client: newNoAuthClient()})
req := files.NewListFolderLongpollArg(cursor)
res, err := noauthdbx.ListFolderLongpoll(req)
if err != nil {
return err
}
if !res.Changes {
continue
}
log.Print("There is a change")
res2, err := dbx.ListFolderGetLatestCursor(reqListFolder)
if err != nil {
return err
}
cursor = res2.Cursor
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment