Monthly Archives: January 2011

How to support OAuth in ASIHTTPRequest

ASIHTTPRequest is a brilliant library, when you want to handle HTTP requests.
At this time, however, this library doesn’t support OAuth.

You can support it easily with OAuthCore library which is developed by atebits who is the developer of Tweetie a.k.a. Twitter for Mac/iPhone.
atebits / OAuthCore / overview – Bitbucket

A sample code is following.

#import "OAuthCore.h"

NSString * const CONSUMER_KEY = @"YOUR_CONSUMER_KEY";
NSString * const CONSUMER_SECRET = @"YOUR_CONSUMER_SECRET";
NSString * const ACCESS_TOKEN = @"YOUR_ACCESS_TOKEN";
NSString * const ACCESS_TOKEN_SECRET = @"YOUR_ACCESS_TOKEN_SECRET";

NSURL *URL = [NSURL URLWithString:@"http://example.com/api"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:URL];

[request setPostValue:@"test" forKey:@"test_value"];
[request buildPostBody];

NSString *header = OAuthorizationHeader([request url],
                                        [request requestMethod],
                                        [request postBody],
                                        CONSUMER_KEY,
                                        CONSUMER_SECRET,
                                        ACCESS_TOKEN,
                                        ACCESS_TOKEN_SECRET);


[request addRequestHeader:@"Authorization" value:header];

If you want to get “Access Token” and “Access Toke Secret”, such as you authorize with xAuth, you should set both ACCESS_TOKEN and ACCESS_TOKEN_SECRET, not nil but @””.

References

xAuth Consumer Sample using Ruby

I’m developing OAuth/xAuth Service Provider using OAuth / OAuth-Plugin.
pelle/oauth-plugin – GitHub

Since OAuth-Plugin doesn’t support xAuth, I decided to create a program to support it.
I’ll write an entry about xAuth support, before that, I write this entry about sample of using xAuth in Ruby.

You need to install Ruby OAuth Gem.

require 'oauth'

CONSUMER_KEY = 'Your-Consumer-Key'
CONSUMER_SECRET = 'Your-Consumer-Secret'

def get_access_token
  consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => 'Your-OAUTH-Provider-URL')
  access_token = consumer.get_access_token(nil, {}, { :x_auth_mode => 'client_auth', :x_auth_username => 'Your-User-Name', :x_auth_password => 'Your-Password' })

  [access_token.token, access_token.secret]
end

def access_api(token, secret)
  consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET, :site => 'Your-OAuth-Provider-URL')
  access_token = OAuth::AccessToken.new(consumer, token, secret)

  access_token.get('Your-API-URL')
end

token, secret = get_access_token
p "token: #{token}"
p "secret: #{secret}"

response = access_api(token, secret)
p response

References

Find Skype Chat ID with rb-skypemac

Each Skype chat has an unique chat ID like “#hogehoge/XXXXXXXXXXXXXXXX”.
This ID has very important role when you handle Skype with another programs/scripts like rb-skypemac (Ruby Gem).
rb skypemac

There is a very simple method to find IDs with rb-skypemac.

irb > require 'rb-skypemac'
 => true
irb > SkypeMac::Skype.send_(:command => 'SEARCH RECENTCHATS')
 => "CHATS #hogehoge/XXXXXXXXXXXXXXXX, #piyopiyo/XXXXXXXXXXXXXXXX"

After this command, you should select the appropriate ID.

Remove extra white-spaces using zsh function

I want to remove easily extra white-spaces and tabs from source codes.
There are a lot of samples using shell scirpts, and I write a tiny script using a zsh function.

I utilize this sample (In Japanese).

funciton rw() {
  for i
  do
    mv $i $i.tmp
    sed -e 's/[[:blank:]]*$//' $i.tmp > $i
    rm $i.tmp
  done
}

This function should be written in your .zshrc file.

To use this script, only you have to do is to type like this.

rw /path/to/*.rb

References