Beispiel #1
0
// Send GET Request to PlurkClient API
func (plurk *PlurkClient) Get(endpoint string, params url.Values) ([]byte, error) {

	requestUri := fmt.Sprintf("%s/%s", plurk.ApiBase, endpoint)
	uri, err := url.Parse(requestUri)
	// TODO(elct9620): Imrpove error handle
	if err != nil {
		return nil, err
	}
	params = signParams(&plurk.credential, "GET", uri, params)
	requestUri = fmt.Sprint(requestUri, "?", params.Encode())
	res, err := http.Get(requestUri)
	logger.Info("GET %s", uri.String())
	logger.Debug("Params %s", params.Encode())
	if err != nil {
		return nil, err
	}

	defer res.Body.Close()
	data, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	if res.StatusCode != 200 {
		var responseError Error
		json.Unmarshal(data, &responseError)
		logger.Error(responseError.ErrorText)
		return nil, errors.New(responseError.ErrorText)
	}

	return data, nil
}
Beispiel #2
0
// Signature from params and url to generate OAuth 1.0 signature
func (c *Credential) Signature(uri *url.URL, method string, params url.Values) string {
	// HMAC-SHA1
	var signatureURL = fmt.Sprintf(
		"%s&%s&%s", // Method&URI&Params
		method,
		url.QueryEscape(uri.String()),
		strings.Replace(url.QueryEscape(params.Encode()), "%2B", "%2520", -1), // Resolve space " " change to "+" after encode
	)

	logger.Debug("Signature String %s", signatureURL)

	key := []byte(fmt.Sprint(c.AppSecret, "&", c.TokenSecret))
	h := hmac.New(sha1.New, key)
	h.Write([]byte(signatureURL))
	return base64.StdEncoding.EncodeToString(h.Sum(nil))
}