简体   繁体   English

使用omniauth-twitter / twitter Gem读取Twitter REST API的x-access-level标头

[英]Reading x-access-Level header for Twitter REST API using omniauth-twitter / twitter Gem

I'm using Omniauth to authenticate users with Twitter through OAuth (using omniauth-twitter gem). 我正在使用Omniauth通过OAuth(使用omniauth-twitter gem)通过Twitter对用户进行身份验证。 I plan to allow normal users to login with just 'read' permissions, and only authorise 'read-write' permissions if/when they decide to create things within the site. 我计划允许普通用户仅使用“读取”权限登录,并且仅当他们决定在网站内创建内容时才授权“读写”权限。

In my Identity model I'm analysing the AuthHash omniauth passes to the create_with_omniauth action, and I'm making separate logic for each provider type, so that I can look deeper into the returned hash schema if necessary. 在我的Identity模型中,我正在分析AuthHash omn​​iauth传递给create_with_omniauth动作,并且我为每种提供程序类型分别制定了逻辑,以便在必要时可以更深入地研究返回的哈希模式。

If I raise auth.to_yaml to output the structure, I see the 'x-access-level' header that I'd like to read, but I don't know how to look into the response: Net::HTTPOK object in order to get to the next level of the structure. 如果raise auth.to_yaml以输出结构, raise auth.to_yaml看到我想读取的“ x-access-level”标头,但我不知道如何查看response: Net::HTTPOK对象按顺序进入结构的下一个层次。

This is the auth structure, cutting out some of the unnecessary details 这是auth结构, 删除了一些不必要的细节

--- !ruby/hash:OmniAuth::AuthHash
provider: twitter
...
extra: !ruby/hash:Hashie::Mash
  ...
  access_token: !ruby/object:OAuth::AccessToken
    ...
    response: !ruby/object:Net::HTTPOK
      http_version: '1.1'
      code: '200'
      message: OK
      header:
        x-access-level:
        - read-write
        x-ratelimit-limit:
        - '350'
        x-ratelimit-remaining:
        - '348'
        x-ratelimit-reset:
        - '1330798604'

So far I can get to the response with auth["extra"]["access_token"].response but putting .header at the end returns the same response structure, and ["header"] is empty when I raise it. 到目前为止,我可以使用auth["extra"]["access_token"].response但是将.header放在末尾将返回相同的响应结构,并且当我引发它时, ["header"]为空。

I'm ok with using the Twitter gem if necessary to do a verify_credentials call inside the Identity model (since Twitter adds the x-access-level header to every response, but even with this approach I wouldn't know how to read the returned headers to read the x-access-level header. 如果有必要,我可以使用Twitter gem来在Identity模型内进行verify_credentials调用(因为Twitter向每个响应中都添加了x-access-level标头,但是即使采用这种方法,我也不知道如何读取返回的内容标头以读取x访问级别标头。

I know this is not the same ruby twitter client but it might help other that arrive here after looking for the same question with the ruby twitter client (gem "twitter"). 我知道这不是同一个ruby twitter客户端,但是它可能会在与ruby twitter客户端(gem“ twitter”)查找相同问题后帮助其他到达此处的人。

So going from this call: 因此,从此调用:

twitter_client = Twitter::Client.new(:oauth_token => token, :oauth_token_secret => secret)

twitter_client.verify_credentials

That will give you only the user info json. 那只会给你用户信息json。 You can actually get the twitter api response and then check the x-access-level contained in the header 您实际上可以获取twitter api响应,然后检查标题中包含的x-access-level

resp = twitter_client.get, "/1.1/account/verify_credentials.json"
resp[:response_headers]["x-access-level"] # "read-write" in my case

Hope this helps others... 希望这对其他人有帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM