Reddit API v1 - Forbidden by administrative rules - ruby-on-rails

Hi I am trying to access my informations via authenticated request to the API.
I am working with Rails to do so, with no sucess, so I tried with Curl and got this:
Request:
curl -X GET -L "https://oauth.reddit.com/api/v1/me?access_token=BLURED_ACCESS_TOKEN" -A "webapp:integrationproject:v1.0 (by /u/Lupins)"
Response:
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
I am trying to do the same with Rails with no success, this is what it looks like in Rails:
user_agent = 'WebApp:IntegrationProject:v1.0 (by /u/Lupins)'
response = RestClient.get "https://oauth.reddit.com/api/v1/me?access_token=#{auth}", user_agent: user_agent
I get the same forbidden error.
Can anyone help me to access this properly?

Solution:
Needed to send this string as header: Authorization: bearer BLURED_ACCESS_TOKEN

Related

HTTP_ORIGIN always returning nil

I'm trying to get the origin of the request cross-site, I found this answer How to get "Origin" request header in Rails which suggest using
request.headers['origin']
I also tried
request.headers['HTTP_ORIGIN']
but both seem to return nil
I'm running this on Rails 6 app
There is nothing wrong with your code.
To get a header in Rails, just access it inside request.headers.
For example, to get User-Agent:
request.headers['User-Agent']
And since rails 5, we have the HTTP_ version also:
request.headers['HTTP_USER_AGENT']
The problem with the Origin header is that it does not always appear in request headers from the browser. It's just needed when the browser sending a cross-origin request.
You can try with some other tools like Postman or curl to see the header is really there:
curl -H "Origin: http://yourserver.com" http://yourserver.com

I want to scrape reddit data using praw. I am getting raise ResponseException(response) error after adding the for loop

subredditcmv=reddit.subreddit('changemyview')
cmv_subreddit=subredditcmv.top(limit=15)
cmv_dict={"Title":[], \
"Score":[], \
"id":[], \
"number_of_comments":[],\
"post":[],\
"created":[]
}
for posts in cmv_subreddit:
cmv_dict["Title"].append(posts.title)
cmv_dict["Score"].append(posts.score)
cmv_dict["id"].append(posts.id)
cmv_dict["number_of_comments"].append(posts.num_comments)
cmv_dict["post"].append(posts.selftext)
cmv_dict["created"].append(posts.created)
receiving this error
File "C:\Users\source\repos\lib\site-packages\prawcore\auth.py", line
31, in _post
raise ResponseException(response)
ResponseException: received 401 HTTP response
The 401 error mean that your request lacks valid authentication credentials for the target resource. You need to authenticate yourself with the reddit api.
However, if you only want to fetch data, you can use the read-only mode or request the data to the reddit api yourself
You are not authenticated with reddit's API (see the PRAW authentication page). Logging in first should fix this issue.

Replicate curl command to access Smartsheet in Rails

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')

Access token does not have the openid scope

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

Google OAuth token exchange returns invalid_code

I have been implementing the Google web server OAuth flow, but when I attempted to exchange the authorization code with access token, it always complains "invalid_code".
Here is the issue:
Step 1:
Redirect one of our pages to 'https://accounts.google.com/o/oauth2/auth?scope=email&redirect_uri=https%3A%2F%2Fmyurl.com%2Fcallback&response_type=code&client_id=some_client_id'
Step 2:
The redirection happens and google would redirect to our url
https://myurl.com/callback?code=somecode
Step 3:
curl -X POST --data "code=somecode&client_id=some_client_id&some_client_secret=some_client_secret&redirect_uri=https://myurl.com/callback&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token -v --trace-ascii /dev/stout
The response comes back:
HTTP 400 Bad request
{
"error" : "invalid_grant",
"error_description" : "Invalid code."
}
Can someone help me with this issue? Thanks!
The life span of authorization code is only 10 mins,and can only be used one time. So do these checks:
Do you use it 10 min later? If so, use it in 10 mins.
Have you used it before? If so, obtain a new one and then use it.
Is you server time in sync with Google OAuth server's? If not, change your time.
I was using http://localhost:8080 as my redirect url since I was just trying out their examples. And my json file contents had this:
"redirect_uris": [
"http://localhost:8080"
],
"javascript_origins": [
"http://localhost:8080"
]
In the developer console I had the redirect_uri set to "http://localhost:8080" and I was getting the same error. I changed it to "http://localhost:8080/" and then it started working. (Essentially adding a '/' at the end.)
Hope this helps!

Resources