Basic Auth POST request to API with Authorized Headers - Ruby - ruby-on-rails

I'm pretty new to submitting POST requests to API's using API credentials and am looking for an explanation on how it's done in Ruby on Rails.
I'm using the Shipwire API and am trying to POST to /orders.
POST /api/v3/orders HTTP/1.1
Host: api.beta.website.com
Authorization: Basic TG9vayBhdCB0aGF0OyBEdWNrcy4uLm9uIGEgbGFrZSEK
I have an Order object ready but am wondering if anyone could shed some light on this.

Httparty gem will help you make any kind of ReST api requests. You will find lots of tutorials on how to perform basic auth using this gem.
However, before jumping on code directly, i would suggest you to understand the request and response on the sandbox mode using the postman plugin for chrome.

Related

How to Parse Json with Bearer Token in Ruby

I have recently graduated a Bootcamp and trying my luck in some projects and applying for jobs.
Got a test, for this potential job, that I don't know how to tackle exactly. At this stage, kind of just wondering how to solve the test.
I need to create a website and display a list of all of the buildings, from this real estate company via their API. They have provided me with the token for the access already.
However, I'm not entirely sure how to connect with their API via Ruby. They gave me an example of how to connect but I haven't seen this before.
curl -H "Authorization: Bearer token_they_provided" https://theirsite/api/buildings.
Any assistance to point me in the right direction will be appreciated.
Thank you all!
What you are trying to achieve is pretty much simple. You can just add HTTP gem (there are other options, like Faraday gem) to your project, which turns everything even simpler.
Using HTTP Gem
Add it to your Gemfile
Install using bundle
Fetches API as below:
response = HTTP[Authorization: "Bearer #{token_they_provided}"].get("https://theirsite/api/buildings")
Parse JSON body:
JSON.parse(response.body)
You can read the gem docs for more information. Also, feel free to connect me via LinkedIn

Authenticate to Google with Oauth2 in JMeter

I am making a POST call to get oAuth token in JMeter. So that I can upload files to Google Drive. Below is my request details in JMeter.
POST https://accounts.google.com/o/oauth2/token
POST data:
client_id=<my_client_id>&auth_uri=https%3A%2F%2Faccounts.google.com%2Fo%2Foauth2%2Fauth&token_uri=https%3A%2F%2Faccounts.google.com%2Fo%2Foauth2%2Ftoken&client_secret=<my_client_secret>&grant_type=authorization_code&redirect_uris=%5B%22urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob%22%2C+%22127.0.0.1%3A3000%22%5D%0A&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive
[no cookies]
Request Headers:
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 424
Host: accounts.google.com
User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_121)
But I am getting below error. Any help is appreciated. Thanks.
{
:
"error"
: : "invalid_request",
: "error_description" : "Missing required parameter: code"
}
I can not help you with JMeter but I can tell you what the error message means.
The grant_type=authorization_code is the second step in the Oauth2 flow. It has a few required parameters one of them is code.
https://accounts.google.com/o/oauth2/token
code=4/X9lG6uWd8-MMJPElWggHZRzyFKtp.QubAT_P-GEwePvB8fYmgkJzntDnaiAI&client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code
The code in question is the code that was returned from the initial request for the user to approve the applications access.
I would also like to add that Google has a number of official client libraries that will deal with these calls for you. Using one of those is much easer then trying to understand the Oauth2 flow if you don't need to. If you cant use the library and you are interested in seeing the full Oauth2 flow to google I have a tutorial on it Google 3 legged Oauth2 flow it shows the pure HTTP calls.
I would recommend using Google OAuth Java Client Library from JSR223 Test Elements using Groovy as a programming language, this is the fastest and the easiest way to obtain/refresh OAuth tokens.
See How to Run Performance Tests on OAuth Secured Apps with JMeter article for detailed explanation and an authorization example.

How to POST text/uri-list using RestKit

it's my first post on StackOverflow :)
We're writing RESTful API backend using Spring Data Rest. To add association we're using the request:
POST /favouriteFolders/7/posts HTTP/1.1
Host: localhost:8080
Content-Type: text/uri-list
http://localhost:8080/posts/68
http://localhost:8080/posts/418
Request format is described here:
http://docs.spring.io/spring-data/rest/docs/2.5.1.RELEASE/reference/html/#_post_2
Our iOS developer has problem sending this kind of request using RestKit
https://github.com/RestKit/RestKit
Any ideas how to use RestKit in this situation?
Or maybe we should use other REST client to handle this specific requests ?
Thanks in advance!

What is the best way to send a SOAP request in Ruby on Rails?

I want to send a SOAP request to a server that requires authentication. I've tried doing some of the methods in the HTTP:NET documentation but basic auth won't work. What would be the best way to contact this server with a user agent? I usually always get a connection time out error (500).
Savonrb gem will be helpful for you in this case.
It is a ruby SOAP client to make SOAP communications easier. Also, check out this Railscasts episode to get started.

Twitter single url request

Is it possible to make a request to Twitter API in a single like? (http request?)
In example you can get the JSON in the Facebook API by the following single url:
https://graph.facebook.com/me?access_token=ACCESS_TOKEN
Can something be possible similarly in twitter. E.g.:
https://api.twitter.com/1.1/statuses/user_timeline.json?consumer_key=CONSUMER_KEY&consumer_secret=CONSUMER_SECRET&access_token=ACCESS_TOEKN&token_secret=TOKEN_SECRET
I find the twitter documentation quite stiff. If somebody has another proposal to get me understand how the communication with the API works I would be glad.

Resources