How can I make a get request to a repository if my user hasn't generated a personal token and I have just the access token from authentication with oauth2?
I have tried some options with postman but I can't fix it.
I want to access a private repository
What I'm trying to do:
require "uri"
require "net/http"
url = URI("https://api.github.com/repos/username/repository_name")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = "Bearer ACCESS_TOKEN"
request["Cookie"] = "_octo=GH1.1.1832634711.1663350372; logged_in=no"
response = https.request(request)
puts response.read_body
One of my collaborators resolve in this way, I hope to be helpful to someone else
def get_github_link
#group=Group.find(params[:id])
if current_user.gh_access_token.nil?
flash[:notice] = "You havn't your GithHub account linked, this is a private repository!"
redirect_to #group and return
end
if #group.status=="private"
url = URI.parse("https://api.github.com/repos/#{current_user.gh_username}/#{#group.git_repository}")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = "token #{current_user.gh_access_token}"
request["Content-Type"] = 'application/json'
request["Accept"] = 'application/vnd.github+json'
request["Coockies"] = 'login-yes'
request.body = {owner: #group.user.gh_username}.to_json
response = https.request(request)
else
url = URI.parse("https://api.github.com/repos/#{#group.user.gh_username}/#{#group.git_repository}")
https = Net::HTTP.new(url.host, url.port)
https.use_ssl = true
request = Net::HTTP::Get.new(url)
request["Authorization"] = "token #{current_user.gh_access_token}"
request["Accept"] = 'application/vnd.github+json'
request.body = {owner: #group.user.gh_username}.to_json
response = https.request(request)
end
#url = URI.parse("https://api.github.com/repos/"+#group.git_url.remove("https://github.com/"))
json = JSON.parse(response.body, symbolize_names: true)
if eval(response.code.to_s) === 200
redirect_to #group.git_url, allow_other_host: true and return
else
flash[:notice] = "You have not the access to this repository, please conctact the admin"
redirect_to #group and return
end
end
Related
I want to reply a review from google app.
This is link document: https://developers.google.com/android-publisher/api-ref/reviews/reply
I use Net::HTTP to post data.
my method to reply review
def self.reply_review(package , access_token, review_id, text )
uri = URI("https://www.googleapis.com/androidpublisher/v2/applications/#{package}/reviews/#{review_id}:reply?access_token=#{access_token}")
puts "https://www.googleapis.com/androidpublisher/v2/applications/#{package}/reviews/#{review_id}:reply?access_token=#{access_token}"
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
params = {
"replyText" => text
}
request = Net::HTTP::Post.new(
uri.request_uri,
'Content-Type' => 'application/json'
)
request.body = params.to_json
response = http.request(request)
puts response
puts(response.body)
response
end
but it always response
=> => #<Net::HTTPBadRequest 400 Bad Request readbody=true>
I am sure my data into accurate (packageName, access_token, replyId).
And how to fix this to reply a review use Net::HTTP
I have used this to connect with google Verification API.
require 'openssl'
require 'net/http'
uri =URI.parse("https://www.googleapis.com/androidpublisher/v2/applications/#{package}/reviews/#{review_id}:reply?access_token=#{access_token}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
request = Net::HTTP::Post.new(uri.request_uri,
initheader = {'Content-Type' =>'application/json'})
request.form_data = {"replyText" => text}
response = http.request(request)
I am using Satelizer for social logins in a rails app.
There's no ruby implementation, so I made one adapted from the node version.
So, after pressing the login with twitter button, I get to my controller, and the following code produces the message: "Bad Authentication data", code: 215
What am I doing wrong?
requestTokenUrl = 'https://api.twitter.com/oauth/request_token'
accessTokenUrl = 'https://api.twitter.com/oauth/access_token'
profileUrl = 'https://api.twitter.com/1.1/users/show.json?screen_name='
requestTokenOauth = {
consumer_key: ENV['TWITTER_KEY'],
consumer_secret: ENV['TWITTER_SECRET'],
callback: ENV['TWITTER_CALLBACK']
};
params = {'oauth' => requestTokenOauth }
require "net/https"
require "uri"
uri = URI.parse requestTokenUrl
http = Net::HTTP.new(uri.host, uri.port)
# https stuff
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data(params)
response = http.request(request)
binding.pry
I'm writing an sms sending function but it doesn't seem to send the sms.
Any idea by sight of this code why?
def self.send_sms(to, from, message)
username = "uname"
password = "pword"
id = rand(36**8).to_s(36)
dlr_url = URI::escape('http://www.skylinesms.com/messages/delivery?id=#{id}&type=%d')
send_url = 'http://localhost:13013/cgi-bin/sendsms?username=#{username}&password=#{password}&to=#{to}&from=#{from}&text=#{message}&dlr-url=#{dlr_url}&dlr-mask=3'
url = URI.parse(URI.encode(send_url))
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }
return res.body
end
I'm not sure but I think so
def self.send_sms(to, from, message)
.....
Net::HTTP.start(url.host, url.port) { |http| http.get(req.request_uri).body }
end
I need to open a signed URL from Amazon S3 and I keep getting access denied.
For example, accessing this URL:
https://aidin.s3.amazonaws.com/aidin/staging/attachments/faxattach/94DD749z65ejkh353?AWSAccessKeyId=AKIAJPBOMKHDQ5WNJM3Q&Expires=1373047778&Signature=Y%2F968F9LIfkfHwsp7T8P0CqjQhQ%3D
From this code:
def download path
local_file = File.new 'attachment' + (Time.now.strftime("%Y%m%d%H%M%S")+(rand * 1000000).round.to_s) + ".pdf", 'wb+'
# HTTPError is caught in the /process in faxattach.rb
uri = URI.parse(path)
http_object = Net::HTTP.new(uri.host, uri.port)
http_object.use_ssl = true if uri.scheme == 'https'
request = Net::HTTP::Get.new uri.request_uri
http_object.start do |http|
response = http.request request
local_file.write(response.read_body)
end
debugger
local_file.rewind
local_file
end
I'm getting this in local_file:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>11A4FB2DF920A272</RequestId><HostId>6qH/f602NxfJa38oW+67Q+5GVx5XD+BJqTzNVR5IhhnvEDhiXUoTR0K90/quZTWk</HostId></Error>
Does anyone know why? The URL opens fine in my browser, and I need the extra query parameters like AWSAccessKeyID, Expires etc., which are there for security reasons.
I would simplify and use OpenURI:
require 'open-uri'
url = "http://aidin.s3.amazonaws.com/aidin/staging/attachments/faxattach/94DD749z65ejkh353?AWSAccessKeyId=AKIAJPBOMKHDQ5WNJM3Q&Expires=1373047778&Signature=Y%2F968F9LIfkfHwsp7T8P0CqjQhQ%3D"
File.open( 'attachment' + (Time.now.strftime("%Y%m%d%H%M%S")+(rand * 1000000).round.to_s) + ".pdf", 'wb+') do |file|
file << open(url).read
end
You want request = Net::HTTP::Get.new(path)
My test in context:
require 'net/http'
path = "https://aidin.s3.amazonaws.com/aidin/staging/attachments/faxattach/94DD749z65ejkh353?AWSAccessKeyId=AKIAJPBOMKHDQ5WNJM3Q&Expires=1373047778&Signature=Y%2F968F9LIfkfHwsp7T8P0CqjQhQ%3D"
uri = URI.parse(path)
http_object = Net::HTTP.new(uri.host, uri.port)
http_object.use_ssl = true if uri.scheme == 'https'
request = Net::HTTP::Get.new(path)
http_object.start do |http|
response = http.request request
puts response.read_body
end
Im trying to use Nets/HTTP to use POST and put in a custom user agent. I've typically used open-uri but it cant do POST can it?
I use
resp, data = Net::HTTP.post_form(url, query)
How would I change this to throw custom headers in?
Edit my query is:
query = {'a'=>'b'}
You can try this, for example:
http = Net::HTTP.new('domain.com', 80)
path = '/url'
data = 'form=data&more=values'
headers = {
'Cookie' => cookie,
'Content-Type' => 'application/x-www-form-urlencoded'
}
resp, data = http.post(path, data, headers)
You can't use post_form to do that, but you can do it like this:
uri = URI(url)
req = Net::HTTP::Post.new(uri.path)
req.set_form_data(query)
req['User-Agent'] = 'Some user agent'
res = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(req)
end
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.value
end
(Read the net/http documentation for more info)
I needed to post json to a server with custom headers. Other solutions I looked at didn't work for me. Here was my solution that worked.
uri = URI.parse("http://sample.website.com/api/auth")
params = {'email' => 'someemail#email.com'}
headers = {
'Authorization'=>'foobar',
'Date'=>'Thu, 28 Apr 2016 15:55:01 MDT',
'Content-Type' =>'application/json',
'Accept'=>'application/json'}
http = Net::HTTP.new(uri.host, uri.port)
response = http.post(uri.path, params.to_json, headers)
output = response.body
puts output
Thanks due to Mike Ebert's tumblr:
http://mikeebert.tumblr.com/post/56891815151/posting-json-with-nethttp
require "net/http"
uri = URI.parse('https://your_url.com')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.ca_path='/etc/pki/tls/certs/'
http.ca_file='/etc/pki/tls/certs/YOUR_CERT_CHAIN_FILE'
http.cert = OpenSSL::X509::Certificate.new(File.read("YOUR_CERT)_FILE"))
http.key = OpenSSL::PKey::RSA.new(File.read("YOUR_KEY_FILE"))
#SSLv3 is cracked, and often not allowed
http.ssl_version = :TLSv1_2
#### This is IMPORTANT
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
#Crete the POST request
request = Net::HTTP::Post.new(uri.request_uri)
request.add_field 'X_REMOTE_USER', 'soap_remote_user'
request.add_field 'Accept', '*'
request.add_field 'SOAPAction', 'soap_action'
request.body = request_payload
#Get Response
response = http.request(request)
#Review Response
puts response.body