forked from mmangino/mogli
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let you exchange a session_key for an access_token
- Loading branch information
Showing
4 changed files
with
138 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,35 @@ | ||
require "cgi" | ||
require 'mogli/client' | ||
|
||
module Mogli | ||
class Authenticator | ||
attr_reader :client_id, :secret, :callback_url | ||
|
||
def initialize(client_id,secret,callback_url) | ||
@client_id = client_id | ||
@secret = secret | ||
@callback_url = callback_url | ||
end | ||
|
||
def authorize_url(options = {}) | ||
options_part = "&" + options.map {|k,v| "#{k}=#{v.kind_of?(Array) ? v.join(',') : v}" }.join('&') unless options.empty? | ||
"https://graph.facebook.com/oauth/authorize?client_id=#{client_id}&redirect_uri=#{CGI.escape(callback_url)}#{options_part}" | ||
end | ||
|
||
def access_token_url(code) | ||
"https://graph.facebook.com/oauth/access_token?client_id=#{client_id}&redirect_uri=#{CGI.escape(callback_url)}&client_secret=#{secret}&code=#{CGI.escape(code)}" | ||
end | ||
|
||
|
||
def get_access_token_for_session_key(session_keys) | ||
keystr = session_keys.is_a?(Array) ? | ||
session_keys.join(',') : session_keys | ||
client = Mogli::Client.new | ||
client.post("oauth/exchange_sessions", nil, | ||
{:type => 'client_cred', | ||
:client_id => client_id, | ||
:client_secret => secret, | ||
:sessions => keystr}) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,60 @@ | ||
require "spec_helper" | ||
describe Mogli::Authenticator do | ||
|
||
let :authenticator do | ||
Mogli::Authenticator.new("123456","secret","http://example.com/url") | ||
end | ||
|
||
it "has the client id" do | ||
authenticator.client_id.should == "123456" | ||
end | ||
|
||
it "has the secret" do | ||
authenticator.secret.should == "secret" | ||
end | ||
|
||
it "has the callback url" do | ||
authenticator.callback_url.should == "http://example.com/url" | ||
end | ||
|
||
it "creates the authorize_url" do | ||
authenticator.authorize_url.should == "https://graph.facebook.com/oauth/authorize?client_id=123456&redirect_uri=http%3A%2F%2Fexample.com%2Furl" | ||
end | ||
|
||
it "creates the authorize_url with scopes as an array" do | ||
authenticator.authorize_url(:scope=>[:user_photos,:user_videos,:stream_publish]).should == | ||
"https://graph.facebook.com/oauth/authorize?client_id=123456&redirect_uri=http%3A%2F%2Fexample.com%2Furl&scope=user_photos,user_videos,stream_publish" | ||
authenticator.authorize_url(:scope=>[:user_photos,:user_videos,:stream_publish]).should == | ||
"https://graph.facebook.com/oauth/authorize?client_id=123456&redirect_uri=http%3A%2F%2Fexample.com%2Furl&scope=user_photos,user_videos,stream_publish" | ||
end | ||
|
||
it "creates the access_token_url" do | ||
authenticator.access_token_url("mycode").should == "https://graph.facebook.com/oauth/access_token?client_id=123456&redirect_uri=http%3A%2F%2Fexample.com%2Furl&client_secret=secret&code=mycode" | ||
end | ||
|
||
it "can trade session_keys for access_tokens" do | ||
Mogli::Client.should_receive(:post). | ||
with("https://graph.facebook.com/oauth/exchange_sessions", | ||
:body => {:type => "client_cred", :client_id => "123456", | ||
:client_secret => "secret", | ||
:sessions => "mysessionkey,yoursessionkey"}). | ||
and_return([{"access_token" => "myaccesstoken", "expires" => 5000}, | ||
{"access_token" => "youraccesstoken", "expires" => 5000}]) | ||
|
||
authenticator. | ||
get_access_token_for_session_key(["mysessionkey","yoursessionkey"]). | ||
should == [{"access_token" => "myaccesstoken", "expires" => 5000}, | ||
{"access_token" => "youraccesstoken", "expires" => 5000}] | ||
end | ||
|
||
it "can trade one session_key for an access_tokens" do | ||
Mogli::Client.should_receive(:post). | ||
with("https://graph.facebook.com/oauth/exchange_sessions", | ||
:body => {:type => "client_cred", :client_id => "123456", | ||
:client_secret => "secret", :sessions => "mysessionkey"}). | ||
and_return({"access_token" => "myaccesstoken", "expires" => 5000}) | ||
|
||
authenticator. | ||
get_access_token_for_session_key("mysessionkey"). | ||
should == {"access_token" => "myaccesstoken", "expires" => 5000} | ||
end | ||
|
||
end |
Oops, something went wrong.