OAuth

facebook\��\��\��������� access_token ������¼������������¹�������������

��돪������������facebook\��\��\��������ҏ�돫����������Ï�����»�ߏ��������������������������³���������������³���������������������������¹��¡��³�����я�������������������¡�

facebook_logo

��������¡����������������Џ�����\½���¼\·\���\���������\������¼\��ď����ҏ����̏����Џ��·�����ď��������������ޏ��������¹��������¡�facebook ���������������½���������ď����������� Graph API �����������������Ï������������̏����С돪؏��µ���������������������������µ����������������������¾��Ï����я�����¼������������¹���������³���������̏��������­���������¹��¡�Graph API ������������\������¼\��������facebook\��⏢�¼\���������¾��Ï����я����̏����㏢���������������Ð������������������¼������������������­���������¹���̏�¡��ӏ��������������������쏢¡ߏ�����¾��Ï����я���������������¾��Ï����я�����������������\������¼\��ď�����¼������������¹�����������ď����������������¾��⏢�̏���̏���ߏ��������������������������¹��¡�

�����¾��⏢���� OAuth 2.0 ������������������»��������������Ώ���»�����������������������������������¡񏪮��¾��⏢�̏��������������Ð access_token ������������������\������¼\���\��я���¼������������������­���������¹��¡��³������������»��������Ï��³������������\������¼\��ď�����\��\���\»\¹���������­���������������ޏ��������¹��������¡�

�����������������Ï��������ޏ�������¡񏪮��¾��⏢�̏���̏���ߏ�����\������¼\���¼���������»��Џ���������������ҏ����������������������������Ώ��������·�����ď�¡�
��\��\��\����������Ð�����ď�����\µ\���\���URL���������Ώ���������»��ӏ���돢��������̏���ߏ��������¹���̏�¡��½���������������������­�������������������������������������·���������¹

1) \������¼\������\��\���\»\¹���¹������
������http://localhost:3000/
2) \µ���¼\Ð�����Ï����������� Graph API ������\������¼\��\������¼\��ď�����������������\���\���\��\¹\������¹������
������https://graph.facebook.com/me
3) OAuthException ���̏����ӏ��µ�����������ď����� authorize \���\½\���\��������ҏ�������������\��\��\��������¾���²��������������������ԏ��Ð���µ����������������
������https://graph.facebook.com/oauth/authorize?client_id=xxx&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F&scope=

facebook\\\¾²

4) µ���²������¹������������\��\���\»\¹���µ������ code ������������������\���\���\������¼\��ď����؏��­������\���\���\��\¹\������̏�����������
������http://localhost:3000/?code=xxx
5) code ������������������\���\���\������¼\��ď����ҏ����؏��ޏ����� access_token \���\½\���\��������ҏ������­��¡�access_token ������¼������������¹������
������https://graph.facebook.com/oauth/access_token?client_id=xxx&client_secret=xxx&code=xxx&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2F
6) access_token �����ҡ돫⏢ď�ԏ��������·��������돩������� Graph API ������\���\���\��\¹\������¹������
������https://graph.facebook.com/me?access_token=xxx

½

¾������·���������ޏ��������­������½��Џ����������������쏢������������¡�

3) �����������¾���²��������������������ԏ��Ð���µ����������������������½���²��я��������ޏ�������¡�\��\��\������̏����¾��⏢�µ���������������Ð��¡�¼���²��я��«������������ 3) ������½������������������¾���²��������������������������ԏ��Ð���µ�����������돢����¼«����돪���쏢���� 4) ������½������������������ԏ��Ð���µ���������������¹��¡�

���������·������������ access_token ������\���\���\­���¼�����������������������򏪤������·�����������쏢�ޏ��Ð��¡��½������������\��Џ��¼\¹���¹���������³������������ 2) ������\��\���\»\¹»��Џ����� access_token �����ҡ돫⏢ď�ԏ��������·�����������؏��ޏ����������²���������³���������������������­���������¹��¡��½�����Ï��¹���������в��ď����㏢����²��ď����㏢����������������¼��������������ҏ��¹����������̏���ߏ����������������������»�����я����� :-)

�����ď��������·��¡�access_token ������������ expires ���̏����Ώ��������µ��������������������������������񏢱������������·������ offline_access ������¼������������¹���������Ð expires ����������Ï����䏪���쏢�������������µ�����������������������������Ï��������¹���̏�����¡��·���Ð���������������¹������������»�����𡯏��������������������������������¹��¡�expires ���̏����⏢������������������¡�access_token �����ҡ돫⏢ď�ԏ��������·���������������·�����������²������������ OAuthException ���������������������³���������̏�������������������¡�돩������� access_token ������¼������������·���������������������µ��������¡�

�����������������Ώ����� expires ���̏����⏢����������������������������²��ď����㏢��������������ޏ����� access_token ����¼������������������­���������¹��¡�

---
��������¡�rest-graph ������������������\���\���\���\���\���������»�����𡯏��Ð���³���������؏�����½������������������叢���������я�����¼«����돢�������������������������������������������������ޏ����������я��������¹���̏�¡�²��ď���¹��ԏ������������������������������������«�������������������̏����㏪�������������������쏢�­�����ď��������������¹��������������������

Ruby������Twitter\���\���\���������OAuth�����¾��⏢����\������¼\��������\���\���\������¼/\���\������¼\���

���µ��������¡񏪱½���������������������쏢�������������¹���̏�¡����������������������� 6·������ӏ����� Twitter \���\���\��������� Basic �����¾��⏢��»�����𡯏����������������������� �����������������á�½���̏��������³���������·���������«���������µ���µ���������«���������������������������¹��¡�

�������������������µ�����������������돢���������������䏪���ӏ��·�����������������ď����я��������¹���̏��ӏ�����������²��������돪�Ð�ޏ�Џ��¹���������«���쏢¡ߏ������돫�����돪����µ������·���������Ð�ޏ�Џ��·���������·�����ď�¡�������¡��������������������������������Ώ������������돪�³�돢�������������ޏ��������¹��¡򏪴��䏪���ӏ��·������������������³�����µ�����я�¡񡭏�����\���������������·�����������Ï�¡�

���½���������½������������ OAuth ������»��������������Ώ��������������������������������³�����������������ҏ�𡭏�������������������µ��������¡�
OAuth\��\���\���\³\���������������ď�������ҏ���������������������²��ҏ��������·�����������Ώ����������� - ������������������������������µ­

��������¡�OAuth »��������Ï�������������������Џ��������쏢���������������Ï��·�����ď��������������������«�����������������������Ï������������³������\µ\���\������������«�����ҏ��«���������Ð���¹���돢�������«������������»�ߏ��������������¹��¡�돪�½������������Ώ��������폢��¼�����³¼�������ӏ��������¹���̏��½���������µ���½��쏢�������������������·��������𡯏��Ð������¡�
Twitter������bot������OAuth���������Ð�ޏ�Џ��µ���»������ - ���·���Ð���½������\����¼\���

����Џ��������쏢����\³���¼\������������³�����я�����������������¡�oauth \���\���\���\���\���������»������������������¹���¼��¡�

require 'oauth'

consumer = OAuth::Consumer.new(
  CONSUMER_KEY, # your consumer key
  CONSUMER_SECRET, # your consumer secret
  :site => 'http://twitter.com'
)

access_token = OAuth::AccessToken.new(
  consumer,
  ACCESS_TOKEN, # your access token
  ACCESS_TOKEN_SECRET # your access token secret
)

# ������������������\���\¹\������¹�����������ԏ����
access_token.post(
  '/statuses/update.json',
  'status' => '\���\���\��½��Џ�����������������������������������'
)

���������ޏ��������¹��������¡��¹���돪�Ð�ޏ�Џ��������­���������·�����ď�¡����ď�������¡�@matometter ������»������������������·��������¡�\������¼\��������\���\���\������¼������\���\������¼\��ӏ�����¹��ԏ�����µ����½���̏������������ď����я��������¹���̏�¡��½������������ OAuth �����¾��⏢����������������¼��������؏��¹�������������«���̏��������«���������������»�����я��쏢�������������������Ï��« oauth \���\���\���\���\���������»����������������̏��������������������«���������������»�����я�����������

������������������\���\¹\������¹�����������򏪳���������ġ������я��������������³������������½��Џ��«�����������������������я��������¹���̏�¡��½�������돪�³�돢����������돫�������������������������������������³��ġ������������������������������������������������ԏ�ď�������⏢�������������¼������»�ߏ����������ď������������³���³������½��Џ����������������쏢�­���������¹��¡�

�����������Ï����� post \���\½\���\��������������������Ï�������\���\½���������������Ï�������¡��³�����¡돫⏢ď�ԏ�������ġ����¹���������������·�����������²�������������������µ���½�����Ï��������¹��¡��������¹���̏�¡�\��\�돢���������������Ώ�����������¾��Ï����я��̏��«���������«���������������«�����������ď�����������¡����쏢���������������������� oauth \���\���\���\���\���������¼������������������­������\½���¼\¹\³���¼\��������ҏ��«�����������Ώ��������·�����ď�¡�

git clone http://github.com/pelle/oauth.git

�����������я����������Ώ����������������������Ï��������³���������؏��������¹����������12¹��ԏ����䏪���آ����������30¹��ԏ����䏪���آ����������¡�¡����������� Net::HTTP ���̏����̏����Џ��µ�����������������������������������Ï��������¹��¡�

# lib/oauth/consumer.rb
def create_http_request(http_method, path, *arguments)
  http_method = http_method.to_sym
          
  if [:post, :put].include?(http_method)
    data = arguments.shift
  end

  headers = arguments.first.is_a?(Hash) ? arguments.shift : {}

  case http_method
  when :post
    request = Net::HTTP::Post.new(path,headers)
    request["Content-Length"] = 0 # Default to 0
  when :put
    request = Net::HTTP::Put.new(path,headers)
    request["Content-Length"] = 0 # Default to 0
  when :get
    request = Net::HTTP::Get.new(path,headers)
  when :delete
    request =  Net::HTTP::Delete.new(path,headers)
  when :head
    request = Net::HTTP::Head.new(path,headers)
  else
    raise ArgumentError, "Don't know how to handle http_method: :#{http_method.to_s}"
  end

  if data.is_a?(Hash)
    form_data = {}
    data.each {|k,v| form_data[k.to_s] = v if !v.nil?}
    request.set_form_data(form_data)
  elsif data
    if data.respond_to?(:read)
      request.body_stream = data
      if data.respond_to?(:length)
        request["Content-Length"] = data.length
      elsif data.respond_to?(:stat) && data.stat.respond_to?(:size)
        request["Content-Length"] = data.stat.size
      else
        raise ArgumentError, "Don't know how to send a body_stream that doesn't respond to .length or .stat.size"
      end
    else
      request.body = data.to_s
      request["Content-Length"] = request.body.length
    end
  end

  request
end

��������������������¡�access_token ���� post ���������« get ���������«������������������������\���\½\���\���������»���������������������������¡�돫⏢ď�ԏ��������·������ URL ������ ����̏����ӏ�����\���\���\������¼\��ď���\���\���\·\���·���¼�돢�������ҏ��������·�����������²���������Ð�����Ï��������������³�����������������������������������������������������Ï��������¹��¡�¾��䏢돪㏢�������«���������������»�����я��̏�¡��³�����Ï�����������������������������\���\������¼\�����¡�

ccess_token.post(
  '/statuses/update.json',
  'status' => '\���\���\��½��Џ�����������������������������������'
)

# ���³�������������돪�²¼���������������Ï�����\��\���\»\¹����¹��ԏ�����������������������������
# HTTP Method: POST
# URL: http://twitter.com/statuses/update.json?status=\���\���\��½��Џ�����������������������������������

�����������������Ï��³��������������¡�Twitter REST API ������\��⏢�¼\��� ���«������\������¼\��������\���\���\������¼/\���\������¼\��ӏ����ҏ��¹������ API �����ҏ���������㏢���������Ώ��������·�����ď�¡�

\������¼\��������\���\���\������¼��¡� Twitter REST API Method: friendships create
\������¼\��������\���\������¼\��ӏ�¡� Twitter REST API Method: friendships destroy

�����������������³������������½¾���������������³���������������Ï��������·�����������²���������Ð oauth \���\���\���\���\���������»�����������������¡�\������¼\��������\���\���\������¼/\���\������¼\��ӏ���¹��ԏ�𡯏��������¹��¡��³������������ (Twitter REST API ������¹��ԏ�𡯏�����) �����������������������돫���¹��ԏ�𡯏����������������돪���ġ����������¹��������¡�

# ���µ���µ�����ď�����������\���\���\������¼
access_token.post(
  '/fendships/create.json',
  'screen_name' => 'sasata299'
)

# ���µ���µ�����ď�����������\���\������¼\��ӏ��������叢���я������������������������������쏢�폢����������¹�����𡯏��������쏢�쏢��
access_token.post(
  '/friendships/destroy.json',
  'screen_name' => 'sasata299'
)
karaage299 at gmail.com
��돪������������µ­»���
\���\��\���\���\���\���