百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
W

weibo_2

> 编程语言
开源

这是一个用于新浪微博 Oauth2 API 的 Ruby 宝石,包含示例,而且确实可以正常运行。

194 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

这是一个用于新浪微博 Oauth2 API 的 Ruby 宝石,包含示例,而且确实可以正常运行。

Note: I got a lot of inspiration from this gem and this gem. The first one will soon be deprecated since weibo is planning to switch to oauth2. The second one has not been fully implemented and is still lacking some features (but a nice try nevertheless). Thanks Scott and acenqiu.

WeiboOAuth2

WeioOAuth2 is a Ruby gem that provides a wrapper for interacting with sina weibo's v2 API, which is currently the latest version. Check out the link if you are interested in browsing the details. The output data format is Hashie::Mash, check out here. If you are not familiar with oauth2, I recommend that you read this article.

Requirements

  1. weibo account
  2. weibo app API key
  3. weibo app API secret

Installation

I call it weibo_2 because someone else took the name weibo_oauth2.

bash
$ gem install weibo_2

Basic Usage

The example have been moved to here. It wiil be more convenient to update.

The example written with sinatra shows how to ask for oauth2 permission, get the token and send status with picture. It should cover basic usage in all ruby apps. You can run your own demo!

bash
$ KEY=change_this_to_your_key SECRET=change_this_to_your_secret REDIR_URI=change_this_to_your_redir_uri ruby example.rb

It should work.

  1. How to get the token?

    OAuth2 is simpler than its first version. In order to get the access token, you first need to get an Authorization Code through a callback request. Now use the following code to get the token.

    ruby
    WeiboOAuth2::Config.api_key = YOUR_KEY
    WeiboOAuth2::Config.api_secret = YOUR_SECRET
    WeiboOAuth2::Config.redirect_uri = YOUR_CALLBACK_URL

    If you are developing in your localhost, you can set YOUR_CALLBACK_URL as 'http://127.0.0.1/callback' something. Then set your weibo app account's callback URL as this URL too. Weibo will call the URL using GET method, which will then enable you to retrieve the authorization code.

    ruby
    client = WeiboOAuth2::Client.new  

    Or you can pass the key and secret to new a client if you did not set WeiboOAuth2::Config

    Redirect to this URL. If user grants you permission, then you will get the authorization code.

    ruby
    client.authorize_url

    In your callback handling method, you should add something like the following,

    ruby
    client.auth_code.get_token(params[:code])

    which will give permission to your client.

  2. How to post a status with picture? (or call other interfaces)

    Simply update a status

    ruby
    client.statuses.update(params[:status])

    Upload a picture.

    ruby
    pic = params[:file].delete(:tempfile)
    client.statuses.upload(params[:status], pic, params[:file])

    pass params[:file] into upload method as options could help weibo_2 to build post body, useful options as:

    • filename, filename with extension of the uploading file, example 'pic.jpg'
    • type, mime type of the uploading file, example 'image/jpeg'

Setting up SSL certificates

This gem using faraday for connection, which supports ssl. According to this article, you can do as following to support ssl connection.

Ubuntu

To locate your SSL certificate folder, type openssl version -a. Append /certs to the OPENSSLDIR listed, here it would be /usr/lib/ssl/certs.

ruby
    client = WeiboOAuth2::Client.new(YOUR_KEY, YOUR_SECRET, :ssl => {:ca_path => "/usr/lib/ssl/certs"})
    # or as below if you have set WeiboOAuth2::Config.api_key and WeiboOAuth2::Config.api_secret already
    # client = WeiboOAuth2::Client.new('', '', :ssl => {:ca_path => "/usr/lib/ssl/certs"})

On Heroku, Fedora, CentOS

ruby
    client = WeiboOAuth2::Client.new(YOUR_KEY, YOUR_SECRET, :ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'})
    # or as below if you have set WeiboOAuth2::Config.api_key and WeiboOAuth2::Config.api_secret already
    # client = WeiboOAuth2::Client.new('', '', :ssl => {:ca_file => '/usr/lib/ssl/certs/ca-certificates.crt'})

For Fedora and CentOS, use the path and file /etc/pki/tls/certs/ca-bundle.crt instead, or find your system path with openssl version -a.

Integrate with Devise and omniauth

  1. Install gems in Gemfile

    ruby
         gem 'devise'
         gem 'omniauth'
      gem 'omniauth-weibo-oauth2'
      gem 'weibo_2'
  2. In devise initailize file config/initiallizers/devise.rb, add a line into setup block, please replace key and secret with yours.

    ruby
      config.omniauth :weibo, 'key', 'secret', :scope => 'user,public_repo'
  3. Then you should handle omini callback controller by yourself, there is sample project show how to integrate devise and omniauth you can follow devise-omniauth-example

  4. After get the callback data, you will see env['omniauth.auth']['credentials'] has the value token and expires_at, store them into session or record,now you can use WeiboOAuth2 in anywhere:

    ruby
      client = WeiboOAuth2::Client.new
      client.get_token_from_hash({:access_token=>session[:token],:expires_at=>session[:expires_at]})
      statuses = client.statuses
      statuses.update('just test from my app')

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言