Git Product home page Git Product logo

weibo_2's Introduction

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

weibo_2's People

Contributors

simsicon avatar swang75 avatar jonnyzheng avatar vvdpzz avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.