- 浏览: 178675 次
- 性别:
- 来自: 北京
最新评论
-
angjunwen:
^[1-9]\d*|0$ 这个能处理小数吗?
ruby on rails 常用正则表达式 -
hot88zh:
Hooopo 写道为什么这么多踩的呢 呃。。还真是,你不说我都 ...
Ruby如何用Oauth与第三方网站互动 -
Hooopo:
为什么这么多踩的呢
Ruby如何用Oauth与第三方网站互动 -
robbinwork:
顶
改良程序的11技巧 -
rogerer:
请问ROR上传大文件,怎么解决内存占用问题:1. lightt ...
查询rails的API
1、注册并认证你的应用
#!/usr/bin/ruby # Get all the auth details you need # You wouldn't actually do it this way, but hey. # Normally you'd distribute the consumer stuff with your # application, and each user gets the access_token stuff # But hey, this is just a demo. require 'rubygems' require 'oauth' require 'yaml' # Format of auth.yml: # consumer_key: (from osm.org) # consumer_secret: (from osm.org) # token: (use oauth setup flow to get this) # token_secret: (use oauth setup flow to get this) auth={} puts "First, go register a new application at " puts "http://api06.dev.openstreetmap.org/oauth_clients/new" puts "Tick the appropriate boxes" puts "Enter the consumer key you are assigned:" auth["consumer_key"] = gets.strip puts "Enter the consumer secret you are assigned:" auth["consumer_secret"] = gets.strip puts "Your application is now set up, but you need to register" puts "this instance of it with your user account." @consumer=OAuth::Consumer.new auth["consumer_key"], auth["consumer_secret"], {:site=>"http://api06.dev.openstreetmap.org"} @request_token = @consumer.get_request_token puts "Visit the following URL, log in if you need to, and authorize the app" puts @request_token.authorize_url puts "When you've authorized that token, enter the verifier code you are assigned:" verifier = gets.strip puts "Converting request token into access token..." @access_token=@request_token.get_access_token(:oauth_verifier => verifier) auth["token"] = @access_token.token auth["token_secret"] = @access_token.secret File.open('auth.yaml', 'w') {|f| YAML.dump(auth, f)} puts "Done. Have a look at auth.yaml to see what's there."
2、创建简单的HTTP请求
#!/usr/bin/ruby # Simple OSM Auth example showing GET PUT and DELETE methods # Requires OAuth rubygem require 'rubygems' require 'oauth' require 'date' require 'yaml' # Format of auth.yml: # consumer_key: (from osm.org) # consumer_secret: (from osm.org) # token: (use oauth setup flow to get this) # token_secret: (use oauth setup flow to get this) auth = YAML.load(File.open('auth.yaml')) # The consumer key and consumer secret are the identifiers for this particular application, and are # issued when the application is registered with the site. Use your own. @consumer=OAuth::Consumer.new auth['consumer_key'], auth['consumer_secret'], {:site=>"http://api06.dev.openstreetmap.org"} # Create the access_token for all traffic @access_token = OAuth::AccessToken.new(@consumer, auth['token'], auth['token_secret']) # Use the access token for various commands. Although these take plain strings, other API methods # will take XML documents. @access_token.put('/api/0.6/user/preferences/demo_last_run_time', DateTime.now().to_s, {'Content-Type' => 'text/plain' }) @access_token.put('/api/0.6/user/preferences/deleteme', "This shouldn't be seen", {'Content-Type' => 'text/plain' }) @access_token.delete('/api/0.6/user/preferences/deleteme') puts @access_token.get('/api/0.6/user/preferences').body
3、表单上传文件
#!/usr/bin/ruby # hacky script for uploading a file to the OSM GPX api. # liberally stolen^Winspired by http://gist.github.com/97756 # # Format of auth.yml: # consumer_key: (from osm.org) # consumer_secret: (from osm.org) # token: (use oauth setup flow to get this) # token_secret: (use oauth setup flow to get this) require 'rubygems' require 'oauth' require 'xml/libxml' require 'date' CRLF = "\r\n" endpoint_gpx_create = 'http://api06.dev.openstreetmap.org/api/0.6/gpx/create' if ARGV.size < 1 puts "Usage: #{$0} path_to_gpx_file" puts "e.g. #{$0} /home/gravitystorm/osm/tracks/20081128.gpx" exit(1) end # Take GPX file from commandline argument file = File.new(ARGV[0]) # Format of auth.yml: # consumer_key: (from osm.org) # consumer_secret: (from osm.org) # token: (use oauth setup flow to get this) # token_secret: (use oauth setup flow to get this) auth = YAML.load(File.open('auth.yaml')) @consumer=OAuth::Consumer.new auth['consumer_key'], auth['consumer_secret'], {:site=>"http://api06.dev.openstreetmap.org"} @access_token = OAuth::AccessToken.new(@consumer, auth['token'], auth['token_secret']) # Encodes the request as multipart def add_multipart_data(req,params) boundary = Time.now.to_i.to_s(16) req["Content-Type"] = "multipart/form-data; boundary=#{boundary}" body = "" params.each do |key,value| esc_key = CGI.escape(key.to_s) body << "--#{boundary}#{CRLF}" if value.respond_to?(:read) body << "Content-Disposition: form-data; name=\"#{esc_key}\"; filename=\"#{File.basename(value.path)}\"#{CRLF}" body << "Content-Type: text/xml#{CRLF*2}" body << value.read else body << "Content-Disposition: form-data; name=\"#{esc_key}\"#{CRLF*2}#{value}" end body << CRLF end body << "--#{boundary}--#{CRLF*2}" req.body = body req["Content-Length"] = req.body.size end # Uses the OAuth gem to add the signed Authorization header def add_oauth(req) @consumer.sign!(req,@access_token) end #Actually do the request and print out the response url = URI.parse(endpoint_gpx_create) Net::HTTP.new(url.host, url.port).start do |http| req = Net::HTTP::Post.new(url.request_uri) add_multipart_data(req,:file=>file, :tags=>'heheheheh', :description=>'upside down', :public=>'1') add_oauth(req) res = http.request(req) puts res.body end
发表评论
-
linux下进入rails console提示cannot load such file -- readline
2011-12-17 20:38 2703在linux下输入rails console,之后提示错误,如 ... -
在Windows7中编译Mysql2的GEM
2011-11-08 15:47 0If you still want to force t ... -
CentOS用gem安装Mysql2提示缺少mysql.h
2011-08-30 12:17 2778环境: CentOS6 Ruby1.9.2 Rails3.0. ... -
Rake提示uninitialized constant Rake::DSL解决办法
2011-06-20 00:09 3674环境:Ruby 1.9.1/Rails 3.0.9/Rake ... -
Debian5安装Thin时候出现no such file to load -- openssl
2011-04-19 22:38 1062今天在执行thin install的时候,出现no such ... -
Ruby如何用Oauth与第三方网站互动
2011-03-13 12:25 2021首先是介绍一下这个gem:oauth 项目首页是:http: ... -
升级gem提示缺少no such file to load zlib
2011-02-20 01:16 1291升级gem提示 no such file to load zl ... -
使用Ruby解析图片EXIF数据获取坐标信息
2011-01-10 08:32 1783最近在做一个项目时需要将图片EXIF信息解析出来并获取相应 ... -
Paperclip提示command is not recognized by the 'identify
2011-01-05 00:43 2244用Paperclip来裁减图片,会提示如下错误: /tmp/ ... -
在Debian上部署Ruby On Rails应用(续)
2011-01-05 00:36 1199写在前面: 其实这个续应该和前面那个部署的文章互换一下顺序… ... -
Ruby1.9.2+Rails3.0.3迁移文件中加索引出错的解决办法
2011-01-03 23:53 1515环境: Ruby1.9.2 Rails3.0.3 Gem ... -
rails3使用declarative_authorization注意事项
2010-11-17 17:32 1162Rails3中把declarative_authorizati ... -
rails3使用restful-authentication
2010-11-09 14:01 1981首先要下载支持Rails3的restful-authentic ... -
Ubuntu安装Mysql Gem
2010-11-03 14:49 1263在安装过程中出现如下错误: Building native e ... -
如何寫出有效率的 Ruby Code
2010-09-28 22:44 991Instance Variables versus Acces ... -
Rails Migration Data Types – MySql – Postgresql – SQLite
2010-06-04 19:09 1149Rails mysql post ... -
request.env
2009-11-11 13:16 1120@client_ip = request.env[" ... -
Active Record Validations and Callbacks
2009-11-08 22:39 1322有许多种在保存数据到 ... -
Ruby on Rails 的检验方法(Validation Helpers)大全
2009-11-06 12:07 1367可以自定义validate(), 这个方法在每次保存数据时都会 ... -
关于ActiveRecord::Observer
2009-11-02 13:58 903Observer 类会对外部的原始类内在生命周期内实现触发行 ...
相关推荐
一个OAuth授权的一个例子,实现了三种常用的授权方式
spring oauth2.0 例子 myeclipse工程
OAuth 2.0 is the next evolution of the OAuth protocol which was originally created in late 2006. OAuth 2.0 focuses on client developer simplicity while providing specific authorization flows for web ...
NULL 博文链接:https://tongxiaoming520.iteye.com/blog/1537016
oauth jersey实现例子,和文档介绍。111
一个springboot为框架的security oauth2应用例子,同时集成了swagger2的 restful API查看页面,druid数据源监控, mybatis自动生成和分页插件,远程资源服务器的认证和授权,也可以去github下载 ...
OAuth 2.0 客户端模式的例子,client_id保存在数据库,而不是写死在配置文件中,支持多个client_id
解决方案包含五个项目 1.WebApiClient:控制台调用接口项目 2.WebApiTest:.NetCore+jwt+swagger编写的接口 3.WebApiTest.ApiOauth2:.Net4.5+oauth2+swagger编写的接口 4.WebApiTest.ApiController:.Net4.5+jwt+...
springboot与security oauth2+jwt控制安全整合例子。。
此资源包含了oauth2.0的鉴权单点登录功能。 部署好tonr和spaklr项目,用tonr项目调用spaklr
Oauth认证的简单例子,很适合新手学习
oauth2.0例子(淘宝)
完整Oauth 2.0 代码实现,包含数据库脚本,使用说明,导入数据库脚本,修改数据库配置可直接运行。
php-oauth一个简单的oauth实现
一、应用场景为了理解OAuth的适用场合,让我举一个假设的例子 二、名词定义在详细讲解OAuth 2.0之前,需要了解几个专用名词 三、OAuth的思路OAut
nodejs 搭建oauth服务器的代码 Install nodejs and npm and then, simply run npm install and npm start. The server should now be running at http://localhost:3000.
omniauth-shopify-oauth2, OmniAuth 1.0的Shopify OAuth2策略 OmniAuth ShopifyOmniAuth 1.0的Shopify OAuth2策略。安装添加到你的Gemfile:gem 'omniauth-shopify-oauth2'然后 bundle instal
OAuth2 -一个OAuth 2.0协议的Ruby封装
易语言新浪微博开源oauth版.zip易语言项目例子源码下载易语言新浪微博开源oauth版.zip易语言项目例子源码下载易语言新浪微博开源oauth版.zip易语言项目例子源码下载 1.合个人学习技术做项目参考 2.适合学生做毕业...
介绍用c#实现 oauth验证的例子 介绍用c#实现 oauth验证的例子