I am trying to access Smartsheet through API 2.0 in a Ruby-on-Rails app. I can get he following curl command to work
curl --request GET https://api.smartsheet.om/2.0/sheets -H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxx"
All attempts to replicate in Ruby code have failed. Can anyone help convert to Ruby?
Thanks!
You have a handful of options to accomplish that. If you want to keep close to curl as you have proposed, you could capture the response of the curl command wraping it with backticks
response = `curl --request GET https://api.smartsheet.om/2.0/sheets -H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxx"`
I'd recommend, however, use something like CURB, which provides Ruby binding for libcurl. It would add some syntax sugar, so your request would look like
response = Curl.get("https://api.smartsheet.om/2.0/sheets") do |http|
http.headers['Authorization'] = 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxx'
end
Yet another option would be ditching curl altogether and use a gem like HTTParty. It makes use of Ruby's net/http to make requests.
That would be something along these lines:
response = HTTParty.get("https://api.smartsheet.om/2.0/sheets",
headers: 'Authorization' => 'Bearer xxxxxxxxxxxxxxxxxxxxxxxxx')
Related
I want to create a "mock" server using Ratpack/Groovy and we need to read the data-raw from a call
I haven't had a problem reading headers or parameters, but I need to read the body of a request
Examplle:
curl --location --POST request 'http://localhost:5050/authenticate' \
--header 'Content-type: application/json' \
--raw-data '{
"username": "testbase01#domain.com",
"password": "12345678"
}'
we need to parse and process the username and password fields
We have tried to read the request.body, or the body.text, but we have not been successful.
Basically the example found in the solution at https://stackoverflow.com/questions/23636248/parse-json-in-ratpack-groovy would be what we need, but it hasn't worked for us either
Our environment has the following dependencies
#Grab('io.ratpack:ratpack-groovy:1.6.1'),
#Grab('org.slf4j:slf4j-simple:1.7.22'),
#Grab("io.ratpack:ratpack-jackson:0.9.17")
and we are using https://github.com/Vad1mo/hello-world-rest as primary image
If you have a small example, to be able to adapt it to our needs
I have SendGrid response and here I get individual X-MessageID which is unique for all sent email, this X-MessageID is actually a starting position of Sendgrid MessageID. So here I was doing something like this "Sendgrid_MessageID Start with X-MessageID",
For.Ex 'KdLh1ZETSQCWx0iqU44BTg.filterdrecv-75d94df84d-4c444-1-627963D1-45.3' start with 'KdLh1ZETSQCWx0iqU44BTg'. I have done this using SendGrid "like" operator like this =>
msg_id like 'KdLh1ZETSQCWx0iqU44BTg%'
and also try another option like
query=msg_id like 'KdLh1ZETSQCWx0iqU44BTg%'
But I get error 'invalid value' for both option, I don't undertstand how to use it. Anyone knows about how to use this "like" operator?
You are using the like operator correctly.
When I make the following HTTP GET request using cURL, I get the message back that start with the given ID:
curl -X GET https://api.sendgrid.com/v3/messages \
--header "Authorization: Bearer [SENDGRID_API_KEY]" \
--data-urlencode "query=msg_id LIKE 'W86EgYT6SQKk0lRflfLRsA%'" \
--data-urlencode "limit=10" --GET
How are you making this HTTP request?
Both GET and POST methods supported by the endpoint. The POST method is recommended to call endpoint with a huge number of user ids to follow, because the GET method will lead to an oversized URL that the server can't handle. How the "follow" parameter can be passed in the body of the request?
UPD: here is what I've already tried using Insomnia (the URL is always 'https://stream.twitter.com/1.1/statuses/filter.json' and the method is always 'POST' and the server response is always "No filter parameters found. Expect at least one parameter: follow track locations"):
A plain text body with Content-Type: text/html
follow=2731236345
A json body with Content-Type: application/json
{
"follow": "2731236345"
}
Another json body
{
"follow": [
2731236345
]
}
However, when I use form-url-encoded with field "follow" and the value "2731236345" I receive the response "Unauthorized".
First of all, consider looking at the Twitter Developer Labs new endpoint, because this existing API will be retired, likely (but not yet confirmed) in 2020.
When you say "without any success", what libraries are you using, and at what levels of query parameters - you're not being very clear about what is not working here. 5000 user IDs is very large. Can you please be more specific about the errors you're seeing, and the code you're trying to run?
I've managed to connect using curl:
curl --request POST \
--url 'https://stream.twitter.com/1.1/statuses/filter.json' \
--header 'authorization: <censored>' \
--data 'follow=2731236345'
The same request doesn't work in Insomnia for some reason, but it doesn't matter for the goal of this post.
I am doing sso sample(travelocity.com) example. When I am trying to access user info with oauth access token using this command,
curl -k -H "Authorization: Bearer b68ba941c9da3d2644d8a63154d28"
https://localhost:9443/oauth2/userinfo?schema=openid
I am getting follwing error
{"error":"insufficient_scope","error_description":"Access token does
not have the openid scope"}
please help, thank you
When you make the first request to the authorization endpoint, you have to include openid in the scope request parameter. OpenID Connect Core 1.0, 3.1.2.1. Authentication Request says as follows.
scope
REQUIRED. OpenID Connect requests MUST contain the openid scope value. If the openid scope value is not present, the behavior is entirely unspecified. Other scope values MAY be present. Scope values used that are not understood by an implementation SHOULD be ignored. See Sections 5.4 and 11 for additional scope values defined by this specification.
For those who tried to put scope in request param and it does not works, put it in the request body in POST /token request
curl --location --request POST 'http://keycloak.local.webapp/realms/WordSpreads/protocol/openid-connect/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=word-spreads-web' \
--data-urlencode 'username=bob' \
--data-urlencode 'password=king' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'scope=openid'
I think the change happen keycloak-1902-released
By default the travelocity.com sso sample web app doesn't sent the openid scope in it's access token request. That is the cause for the error you have encountered.
In order to send the openid scope along with the access token request in the travelocity sample you can try the following,
Open travelocity.properties[1] file in the sample web app (You can find it in travelocity.com/WEB-INF/classe)
Uncomment and edit the QueryParams property in the file[1] as shown below
QueryParams=scope=openid
Save the properties file and redeploy the web app and try the access token generated on the userinfo endpoint now :)
[1] https://github.com/wso2/product-is/blob/master/modules/samples/sso/sso-agent-sample/src/main/resources/travelocity.properties
Update
Looks like the setting the scope in QueryParams isn't working,
There's a workaround
Can you change OAuth2.TokenURL in travelocity.propeties as below and try out? I tested this locally and should work.
#OAuth2 token endpoint URL
OAuth2.TokenURL=https://localhost:9443/oauth2/token?scope=openid
I am using Ruby on Rails 4.1 and I am trying to implement an API with a custom mime type. That is, in config/initializers/mime_types.rb I register an alias as like the following:
Mime::Type.register_alias 'application/json', :my_json
From another system I am trying to access the API with curl by running a HTTP PUT request, this way:
curl http://www.my_api.org/articles.my_json --request PUT --header "Content-Type: application/json" --data-binary '{\"key\": {\"a\": \"1\", \"b\": \"2\"}}'
However, by inspecting the ArticlesController parameters in my Rails application, I get the following output (note: article parameters are "unwanted" and those duplicate the "wanted" key parameters):
Parameters: {"key": { "a"=>"1", "b"=>"2" }, "article": { "a"=>"1", "b"=>"2" }}
What is the problem? Is it a bug? How can I solve that?
Note: I have implemented and access other similar API by executing HTTP GET requests and all works as expected. The problem seems to happens only when I execute HTTP PUT requests.
#rafaelfranca - No it is not a bug. It is how wrap_parameters works. You can disable at this file in your application config/initializers/wrap_parameters.rb.
See github.