Last active
August 29, 2015 14:17
-
-
Save SunRunAway/ee9367085473b841450e to your computer and use it in GitHub Desktop.
sign a qiniu request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"io" | |
"net/http" | |
"os" | |
"strings" | |
"github.com/qiniu/api/auth/digest" | |
) | |
type nullTransport struct { | |
} | |
func (t *nullTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) { | |
return nil, nil | |
} | |
func signRequset(ak, sk string, urlStr, mine string, body io.Reader) (token string, err error) { | |
mac := &digest.Mac{ | |
AccessKey: ak, | |
SecretKey: []byte(sk), | |
} | |
req, err := http.NewRequest("POST", urlStr, body) | |
if err != nil { | |
return | |
} | |
req.Header.Set("Content-Type", mine) | |
tr := digest.NewTransport(mac, &nullTransport{}) | |
_, err = tr.RoundTrip(req) | |
if err != nil { | |
return | |
} | |
token = req.Header.Get("Authorization") | |
return | |
} | |
func main() { | |
AK := flag.String("ak", "", "access key") | |
SK := flag.String("sk", "", "secret key") | |
URL := flag.String("url", "", "url to sign") | |
BODY := flag.String("body", "", "body to sign") | |
MIME := flag.String("mime-type", "application/x-www-form-urlencoded", "content type") | |
flag.Parse() | |
if *AK == "" { | |
fmt.Fprintln(os.Stderr, "AK is empty") | |
} | |
if *SK == "" { | |
fmt.Fprintln(os.Stderr, "SK is empty") | |
} | |
if *URL == "" { | |
fmt.Fprintln(os.Stderr, "URL is empty") | |
} | |
if *BODY == "" { | |
*MIME = "" | |
} | |
token, err := signRequset(*AK, *SK, *URL, *MIME, strings.NewReader(*BODY)) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
} | |
fmt.Println(token) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment