首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > Ruby Rails >

人们api(ruby)

2012-09-22 
人人api(ruby)#encoding: utf-8require net/httprequire net/httpsrequire jsonrequire oauth2cla

人人api(ruby)

#encoding: utf-8require "net/http"require "net/https"require "json"require "oauth2"class RenRenController < ApplicationController    def client        #得到Authorization Code        ac = OAuth2::Client.new(api_key, api_secret, "/oauth/authorize","/oauth/token",            :site => {:url=>'https://graph.renren.com', :response_type=>'code'},            :access_token_url => 'https://graph.renren.com/oauth/token')    end    def login        # scope赋予不同的权限,比如发表文章就需要特定权限,这个要去api去查看        redirect_to client.auth_code.authorize_url(                                        :scope=>"publish_feed publish_blog",                                        :redirect_uri => "项目的url/loginnext",                                        :response_type=>'code')    end    def loginnext        #得到Access Token        access_token = client.auth_code.get_token(params[:code], {:redirect_uri =>"项目的url/loginnext"})        session[:renren_access_token]=access_token.token        #获得Session Key,为调用renren api做准备           get_uri=URI.parse(URI.encode("http://graph.renren.com/renren_api/session_key?oauth_token=#{session[:renren_access_token]}"))         res=JSON Net::HTTP.get(get_uri)        session[:renren_session_key]= res["renren_token"]["session_key"]        session[:renren_expires_in]=res["renren_token"]["expires_in"]        session[:renren_refresh_token]=res["renren_token"]["refresh_token"]        redirect_to "/ren_ren/add_blog"    end       def add_blog        session_key=session[:renren_session_key]        str="api_key=#{api_key}"        str<<"content=testbdesc"        str<<"format=JSON"        str<<"method=blog.addBlog"        str<<"session_key=#{session_key}"        str<<"title=testb"        str<<"v=1.0"        str<<"#{api_secret}"        #创建sig, 注意顺序, 这些参数必须是按照首字母英文a-z的顺序        sig=Digest::MD5.hexdigest(str)        query={:api_key=>api_key,                     :content=>'testbdesc',                     :format=>'JSON',                     :method=>'blog.addBlog',                     :session_key=>session_key,                     :title=>'testb',                     :v=>'1.0',                     :sig=>sig        }        @ab=JSON Net::HTTP.post_form(URI.parse(URI.encode("http://api.renren.com/restserver.do")),query).body    end    def user        session_key=session[:renren_session_key]        str="api_key=#{api_key}"        str<<"call_id=#{Time.now.to_i}"        str<<"format=JSON"        str<<"method=users.getInfo"        str<<"session_key=#{session_key}"        str<<"v=1.0"        str<<"#{api_secret}"        sig=Digest::MD5.hexdigest(str) #create sig        query={:api_key=>api_key,                     :call_id=>Time.now.to_i,                     :format=>'JSON',                     :method=>'users.getInfo',                     :session_key=>session_key,                     :v=>'1.0',                     :sig=>sig        }        @user=JSON Net::HTTP.post_form(URI.parse(URI.encode("http://api.renren.com/restserver.do")),query).body    end    #api_key    def api_key        'api_key'    end    #secret_id    def api_secret         'secret'    endend
?

?

很多东西要去查看renren的api,这里用到了几个gem, json和oauth2,别忘记引入http和https

热点排行