drot98385 2016-05-11 07:05
浏览 147

Golang单足/两足oauth 1.0a身份验证

I am looking for a library to help me authenticate with a service via oauth 1.0a using one-legged authentication. I only have a consumerKey, consumerSecret and resource url.

Examples are available in curl, PHP and Python. I am trying to authenticate with GoLang.

curl
-X GET
-H "Content-Type: application/json"
-H 'Authorization: OAuth realm="https://example.com",oauth_consumer_key="1234-5678-91011",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1424789327",oauth_nonce="abcde",oauth_version="1.0",oauth_signature="3iwWBq%2FHaBcDl9Q1o%2BjAc%2BKz8J%2FE%3D"'
https://example.com

php

require 'vendor/autoload.php';

use Guzzle\Http\Client;
use Guzzle\Plugin\Oauth\OauthPlugin;

$client = new Client('https://example.com');
$oauth = new OauthPlugin(array(
    'consumer_key'    => '1234-5648-9101',
    'consumer_secret' => 'abcde',
));
$client->addSubscriber($oauth);

$response = $client->get('/')->send();
echo $response->getBody();

Python

from requests_oauthlib import OAuth1Session

key = '1234-5648-9101'
secret = 'abcde'
example = OAuth1Session(key, client_secret=secret)
url = 'https://example.com'
r = example.get(url)
print r._content

I have looked at many oauth libraries in Golang.

https://github.com/mrjones/oauth, requires: RequestTokenUrl, AccessTokenUrl, AuthorizeTokenUrl and callback url.

http://godoc.org/github.com/nhjk/oauth, requires: a tokenKey and tokenSecret as well as the consumerKey and consumerSecret.

Is there a library I can use which will allow me to authenticate with the service just as easily as php and python examples above? If i can use one of libraries above, could somebody provide a code snippet example? Anything I try just returns unauthorized responses.

  • 写回答

1条回答 默认 最新

  • dongwh1992 2016-05-11 11:15
    关注

    Golang has a different aproach for frameworks, for one reason, you do not need them. You can easyily set the header of the response with the standard http package. When you get the hang of it, you realize that you are dealing with first class objects, which is what those other language frameworks are trying to implement.

    Something like this function

      func saveHandler(w http.ResponseWriter, r *http.Request) {
        // allow cross domain AJAX requests
        w.Header().Set("Access-Control-Allow-Origin", "*")
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画