JSON interaction after OAuth2/OmniAuth authentication - ruby

Context: I'm trying to interact with Twitter via JSON and no libraries as i'm practicing to interact with a newly released receipt printer(themprinter.com) which has no helper libraries. I need to OAuth with the printer then make the appropriate calls to register my device, print, verify online/offline status etc.
I've successfully authenticated with Twitter via OmniAuth's Twitter Gem. I can pull all the data from the Authentication Hash here - https://github.com/arunagw/omniauth-twitter
...now what? I want to be able to make a JSON call with my OAuth credentials to Twitter and pull my timeline or any other such data. Can anyone provide any sample code that will allow me a starting point to tinker with and work off of?

Twitter provides REST API. Here's how you might create a GET REST request
request = Net::HTTP::Get.new(url, initheader = header)
http_request = Net::HTTP.new(host, port)
response = http_request.start {|http| http.request(request)}
here's an example of the request URL:
https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=twitterapi&count=2

Related

Intercept HTTP Request in page

I am trying to make a scraper for a page of a supermarket. I noticed that this supermarket make API HTTP calls via AJAX, and if I enter to Inspect > Network then I can see the request headers of the calls I need.
Inside the requests headers there is an X-Token. If I use this X-Token with the URL via Postman I can get all the info in JSON format, which is better than scraping the web.
The problem is that this X-Token expires (I think, it still works). Is there any possibility to make a call to the page and "intercept" this API call in order to retrieve this X-Token and use it for the next custom requests?
I'am using Ruby on Rails :)

How to perform a GET request after a POST request with web-sockets

I am currently trying to build a go API using gin for a web and mobile application. I am new to the world of WebSockets and Go so I was wondering how I would go about triggering a GET request from the client after a relevant POST request was made ie: the POST request contained the user's ID so the clients who require information regarding that user are properly updated. Currently, I have the POST and GET requests which do what I need them, but I'm a little lost about how to make the entire flow realtime using WebSockets.
I believe this example of server-sent-events should address the question. Once a POST handler has been called, send a flag to the GET endpoint via a channel and then send an event through there.

Is it possible to send a POST request to external API when receiving one?

I'm receiving a JSON POST request from my frontend and store it in my database - this is standard behavior of my backend.
Is it possible to send the same data (or some parts of it, after validation) with another POST request to an external API that is not managed by me? If so, I think it would be by extending create method - am I correct? How could I trigger sending a request to that 3rd Party API on receiving it in my backend?
Do you know any examples?
Yes, You can do that using pip install requests. It means your api is working as a proxy.
Example
from rest_framework import Response
import requests
def api_view(request):
external_api_url = 'https://example.com/api/endpoint/'
data = request.POST
res = requests.post(external_api_url, data)
return Response(res.json())
You can create (override) your custom "create" view method, adding the call to the external API (if the external API is REST, "request" is a good tool).
Establish a new HTTP conections (external) can penalize the endpoint response speed, and if the external service is too slow, can produce timeout so I suggest do the external api call using async mechanism, (celery task worker or async functions) and ensure with a retrive loop and timeout.
Using the requests library, You can POST data to an external API
For Example
import requests
request_type = "POST"
data = {"email":"email", "name": "name"}
api_url = "https://external-api.com/api/post/"
response = requests.request(request_type, api_url, data=data)

How to hit gmail end points from postman?

I need to hit gmail end points to send emails and get the email details. I got one url which can be used to send and get the emails i.e. google api explorer. https://developers.google.com/gmail/api/v1/reference/users/messages/get
But it's not written here about the end points. So somebody please explain what things are required to send and retrieve the email by using gmail api end points through postman?
The end point for sending messages can be found on the documentation page.
User.messages.send there is also information about the post body.
Upload URI, for media upload requests:
POST https://www.googleapis.com/upload/gmail/v1/users/userId/messages/send
Metadata URI, for metadata-only requests:
POST https://www.googleapis.com/gmail/v1/users/userId/messages/send
authentcation
You are going to have to set up authnecation as well. the easest way to do this will be to use the Oauth 2.0 play ground to get a bearer token and apply that to your calls within postman

Create function in Parse Cloud Code that does not require authorisation

I have my own instance of Parse Server running on AWS and until now Cloud Functions have been working great, but with one caveat: they cannot be successfully called publicly, i.e. they require an authorisation key be sent in the REST request header.
I want to set up a Slack Slash Command to my server, and it has to be able to POST a payload without any headers or extra parameters. As a result, my requests are currently unauthorised (returning 403 statuses).
Is there a way to create granular control over a Parse Cloud Function's authorisation (i.e. if it requires master-key header or not), and if not — is there a way of forwarding the request but still through the Parse server?—Or even a way of manipulating the headers of a Slack request? I would rather not have to use another service just for request forwarding.
Thanks!
Two options
Pass in the master key on the client request which should bypass authorization. It's a blunt approach but might be okay in your case (without knowing more details).
Or run a new express endpoint alongside parse and from there call the parse cloud function using the masker key.
var api = new ParseServer(...)
var app = express();
app.use('/parse', api);
app.get('/api/slack', function(req, res) {
//call cloud function passing in master key
// add X-Parse-Master-Key as http header
unirest.post("http://myhost.com:1337/parse/functions/mycloudfunction")
.headers({'X-Parse-Master-Key', MASTER_KEY)
.end(function(response) {
}

Resources