Giter Site home page Giter Site logo

goauth's Introduction

Installation

goinstall github.com/reinaldons/goauth

Usage

import (
    "github.com/reinaldons/goauth"
    "os"
)

func someFuncThatDoesStuffWithOAuth() (err os.Error) {
    o := new (oauth.OAuth)
    o.ConsumerKey = "key"
    o.ConsumerSecret = "secret"
    o.Callback = "callback"

    o.RequestTokenURL = "https://api.twitter.com/oauth/request_token"
    o.OwnerAuthURL = "https://api.twitter.com/oauth/authorize"
    o.AccessTokenURL = "https://api.twitter.com/oauth/access_token"

    err = o.GetRequestToken()
    if err != nil { return }

    url, err := o.AuthorizationURL()
    if err != nil { return }

    // somehow send user to url...

    var verifier string
    // somehow get verifier (or "PIN")...

    err = o.GetAccessToken(verifier)
    if err != nil { return }

    err = o.Save(os.Getenv("HOME") + "/.simple_example.oauth")

    response, err := o.Post(
        "https://api.twitter.com/1/statuses/update.json",
        map[string]string{"status": "Just did a little OAuth dance!"})
    if err != nil { return }

    // do stuff with response...

    return nil
}

With access token and key saved

o := new(oauth.OAuth)
o.ConsumerKey = "app key"
o.ConsumerSecret = "app secret"
o.SignatureMethod = "HMAC-SHA1"

o.AccessToken = "user access token"
o.AccessSecret = "user access secret"

get := make(map[string]string)
header := make(map[string]string)
response, err := o.Post("http://api.dropbox.com/0/account/info", "", get, header)
if err != nil {
        fmt.Println("AccountInfo fail: ", err)
        return
}

fmt.Println("Response Body: ", bodyString(response.Body))

Upload file

func UploadFile(path string, filename string, content string) {
    boundary := fmt.Sprintf("mandic.gomda.%d", time.Nanoseconds())
    
    body := `--[--boundary--]
    Content-Disposition: form-data; name="file"; filename="[--filename--]"
    Content-type: text/plain
    
    [--content--]
    --[--boundary--]--
    
    `
    
    body = strings.Replace(body, "[--boundary--]", boundary, 2)
    body = strings.Replace(body, "[--filename--]", filename, 1)
    body = strings.Replace(body, "[--content--]", content, 1)
    
    get := make(map[string]string)
    get["file"] = filename
    header := make(map[string]string)
    header["User-Agent"] = "Mandic Dropbox"
    header["Content-Length"] = fmt.Sprintf("%d", len(body))
    header["Content-Type"] = fmt.Sprintf("multipart/form-data; boundary=%s", boundary)
    
    o := new(oauth.OAuth)
    o.ConsumerKey = "app key"
    o.ConsumerSecret = "app secret"
    o.SignatureMethod = "HMAC-SHA1"
    
    o.AccessToken = "user access token"
    o.AccessSecret = "user access secret"
    
    response, err := o.Post(fmt.Sprintf("http://api-content.dropbox.com/0/files/dropbox/%s", path), body, get, header)
    if err != nil {
            fmt.Println("UploadFile fail: ", err)
            return
    }
    
    fmt.Println("Response Body: ", bodyString(response.Body))
}

Status

This is still a bit of a work in progress. The interface is subject to change (and become prettier), but I think it's mostly done.

It is probably obvious that it's rough around the edges in spots. Please let me know if anything's broken.

goauth's People

Contributors

alloy-d avatar

Stargazers

Reinaldo Nolasco Sanches avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.