gpt4 book ai didi

ruby-on-rails - ruby 和推特 : Getting Access Token from Request Token?

转载 作者:太空宇宙 更新时间:2023-11-03 17:52:40 25 4
gpt4 key购买 nike

我目前正处于从尝试通过 Twitter 登录的用户那里获取 oauth token / secret 的处理的第 3 步。 https://dev.twitter.com/docs/auth/implementing-sign-twitter

第 3 步告诉我将此请求发送到 API,但我不知道该怎么做。我目前同时拥有 oauth_token 和 oauth_verifier,但是如何发送此 POST 请求以获取 oauth_token、oauth_token_secret 对?

是否有可用于发送此 POST 请求的标准 Oauth Ruby gem?我在网上看到我传递 @accessToken 对象的示例,但我没有这样的对象可用。我只有 oauth_token 和 oauth_verifier(作为字符串)。鉴于这两件事,我如何将它们转换为 oauth_token 和 oauth_token_secret?

POST /oauth/access_token HTTP/1.1
User-Agent: themattharris' HTTP Client
Host: api.twitter.com
Accept: */*
Authorization: OAuth oauth_consumer_key="cChZNFj6T5R0TigYB9yd1w",
oauth_nonce="a9900fe68e2573b27a37f10fbad6a755",
oauth_signature="39cipBtIOHEEnybAR4sATQTpl2I%3D",
oauth_signature_method="HMAC-SHA1",
oauth_timestamp="1318467427",
oauth_token="NPcudxy0yU5T3tBzho7iCotZ3cnetKwcTIRlX0iwRl0",
oauth_version="1.0"
Content-Length: 57
Content-Type: application/x-www-form-urlencoded

oauth_verifier=uw7NjWHT6OJ1MpJOXsHfNxoAhPKpgI8BlYDhxEjIBY

最佳答案

使用 twitteroauth gem 尝试类似以下 Rails Controller 操作:

  def redirect
consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, {
:site => "https://api.twitter.com",
:scheme => :header
})

request_token = consumer.get_request_token(:oauth_callback => CALLBACK_URL)
session[:twitter_request_token] = request_token

redirect_to request_token.authorize_url #=> "https://api.twitter.com/oauth/authorize?oauth_token=XYZ"
end

def callback
request_token = session[:twitter_request_token]

access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
client = Twitter::REST::Client.new(
:consumer_key => CONSUMER_KEY,
:consumer_secret => CONSUMER_SECRET,
:access_token => access_token.token,
:access_token_secret => access_token.secret
)
twitter_user = client.user

redirect_to root_url # or do something with the twitter_user
end

另请参阅:http://barkingiguana.com/2009/10/13/twitter-oauth-authentication-using-ruby/

关于ruby-on-rails - ruby 和推特 : Getting Access Token from Request Token?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21369244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com