Skip to content

dingzhihu/weibo_2

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

$ gem install weibo_2

Basic Usage

The example written with sinatra in this directory 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 visit http://weibo-oauth2-example.herokuapp.com/ to see the demo.

update: Sorry, the api key I used for this demo is still in the process of auditing by weibo side, it's not available now for connecting. But you can apply your own api key, clone the example in this directory, then type

$ 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.

    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.

    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.

    client.authorize_url

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

    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

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

    Upload a picture

    tmpfile = params[:file][:tempfile]
    pic = File.open(tmpfile.path)
    client.statuses.upload(params[:status], pic)

Setting up SSL certificates

This gem using [faraday](https://github.com/technoweenie/faraday) for connection, which supports ssl. According to [this article](https://github.com/technoweenie/faraday/wiki/Setting-up-SSL-certificates), 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

    	 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.

      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:

      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')

About

A ruby gem for sina weibo oauth2 api, with examples, and it actually works.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published