Yammer-oauth2

An Yammer OAuth2 client

View the Project on GitHub tiabas/yammer-oauth2

Yammer OAuth2 Client

Gem Version Build Status Dependency Status Coverage Status

An Ruby wrapper for Yammer's OAuth2 implementation

For more about the OAuth2 standard checkout: http://tools.ietf.org/html/rfc6749

Installation

gem install yammer-oauth2

Resources

Usage Examples

require 'yammer-oauth2/client'

yammer_client = YammmerOAuth2::Client.new('PRbTcg9qjgKsp4jjpm1pw', 'Xn7kp7Ly0TCY4GtZWkmSsqGEPg10DmMADyjWkf2U')

Authorization Grants

The client wraps around the creation of any given grant and passing in the parameters defined in the configuration file. The supported grants include Authorization Code and Implicit. They are available via the authorization_code and implicit methods on a client object.

Authorization Code

# generate authorization url
auth_url = yammer_client.webserver_authorization_url
# => https://www.yammer.com/dialog/oauth/authorize?client_id=PRbTcg9qjgKsp4jjpm1pw&response_type=code

# exchange authorization code for access token. we will get back a Net::HTTPResponse
response = yammer_client.exchange_auth_code_for_token({
    :code => '11a0b0b64db56c30e2ef',
    :redirect_uri => 'https://localhost/callback',
  })

response.inspect 
# => #<Net::HTTPOK:0x007ff8bc7c1200>

response.body
# => {
#      "access_token" : "e409f4272fe539166a77c42479de030e7660812a",
#      "token_type" : "bearer"
#    }"

Implicit Grant

auth_url = yammer_client.clientside_authorization_url(:redirect_uri => 'http://localhost/oauth2/callback')
# => "https://www.yammer.com/dialog/oauth/?client_id=PRbTcg9qjgKsp4jjpm1pw&redirect_uri=http%3A%2F%2Flocalhost%2Foauth%2Fcallback&response_type=token"

Using a custom Http wrapper

By default, yammer-oauth2 uses the oauth2-client gem's HTTP wrapper OAuth2::HttpConnection. However, if you wish to use a different HTTP library, you only need to create a wrapper around your favorite library that will respond to the send_request method.

class TyphoeusHttpConnection

  def initialize(site_url, connection_options={})
    # set url and connection options
    @site_url = site_url
    @connection_options = connection_options
  end

  def base_url(path)
    @site_url + path
  end

  def send_request(http_method, request_path, options={})
    # options may contain optional arguments like http headers, request parameters etc
    # send http request over the inter-webs

    params          = options[:params] || {}
    headers         = options[:headers]|| {}
    method          = method.to_sym
    client          = Typhoeus

    case method
    when :get, :delete
      #pass
    when :post, :put
      options[:body] = options.delete(:params) if options[:params]
    else
      raise UnhandledHTTPMethodError.new("Unsupported HTTP method, #{method}")
    end
    response = client.send(http_method, base_url(request_path), params)
  end
end

# now you can initialize the OAuth2 client with you custom client and expect that all requests
# will be sent using this client
oauth_client = YammerOAuth2::Client.new('example.com', client_id, client_secret, {
  :connection_client  => TyphoeusHttpConnection,
  :connection_options => { :use_ssl => true }
})

Supported Ruby Versions

This library aims to support and is tested against the following Ruby version:

This library may inadvertently work (or seem to work) on other Ruby implementations, however support will only be provided for the versions listed above.

Copyright

Copyright (c) 2013 Kevin Mutyaba See LICENSE for details.