Problem sending web API to Spotify using Laravel and Guzzle package - laravel

I'm trying to communicate with Spotify using web API.
So here's the case. In order to retrieve any information from Spotify, first I need to be authorized by Spotify. To do so, I need to send a post request to https://accounts.spotify.com/api/token containing authorization credentials(containing client_id and client_secret), and then in response, I should receive an access token which later I shall use to retrieve any other information.
Here's a quick Spotify documentation on how it works :
So the problem here is that I'm doing everything I should but I don't get the token in response.
Here's my code using the Guzzle package in laravel :
And instead of an access token as a response, This HTML output is what I'm receiving from Spotify in return (Without any further information of the problem and any error code):

It is possible that you are misinterpreting this line?
Authorization: Basic <base64 encoded client_id:client_secret>
It looks like you are doing this:
base64_encode($client_id) . ":" . base64_encode($client_secret)
But perhaps they want this:
base64_encode($client_id . ":" . $client_secret);
That is, assuming you have base 64 encoded them at all as this is not actually shown in your code.
Additionally, the documentation states that it wants application/x-www-form-urlencoded encoding.
# Sending Form URL Encoded Requests
https://laravel.com/docs/8.x/http-client#sending-form-url-encoded-requests
To meet this requirement, it looks like you may need to add asForm() to the request.
$response = Http::withHeaders(...)->asForm()->post(...);

Related

Handling text/plain request in Laravel (sent via navigator.sendBeacon)

Problem:
I am trying to get the content / body from a text/plain POST request in a Controller in Laravel 8.
Nothing in the docs or numerous google searches worked.
I have tried:
$request->input();
$request->all();
$request->json();
json_encode($request);
All of these seem to show that the request is completely empty.
Context
This may not be relevant to the solution, but might help other's who are trying to google this problem: The request I am trying to handle with my controller is being sent by navigator.sendBeacon on the client side. The request body is actually stringified JSON, but sendBeacon does not allow you to send requests with content-type JSON. Devtools show the content-type header for this request as 'text/plain'.
My Answer:
Use $request->getContent() to get the content of a text/plain HTTP request.
And then, if you are in a situation like mine where the text is actually JSON, use json_decode($request->getContent(),true) to get a standard PHP array from the content which you can use in your Controller.
It turned out to be quite simple, but the information was not in the docs, or in any online searches so I thought it would be worthwhile to post to SO anyways...
You can send the data as JSON like this instead
data = new Blob([JSON.stringify(data)], {type : 'application/json'})
navigator.sendBeacon('/your/route/here', data)

Rest End point not showing any json data when using POSTMAN application

I am trying to implement sample spring boot project and trying to retrieve result using POSTMAN application. But when using POSTMAN , I am not able to see that response for a GET request. But I can see by using browser properly. POSTMAN returning "Expected ':' instead of 't'" as result. But I can see result properly by using browser.
And my controller code is like the following,
#GetMapping("/check")
public List<Users> check() {
return (List<Users>) userObj.findAll();
}
Can anyone help me to find out why I am not able to see result using POSTMAN application please?
You are saying to postman (for that matter any client) that your api is producing json and instead you are returning just a string.
Check your header you will have a field called
Content-Type → application/json;charset=UTF-8
All you need to do is ensure you are sending valid json or remove that header entry so clients do not try to read it as json.
Or just to check you are getting right data by changing format from json to text in postman
You server application needs to understand the incoming request type. can you define the Content-Type=application/json and give a try.

How to authenticate user when testing REST API using Jmeter

I am trying to make a script to test REST services using Jmeter.
Till now I was using Chrome’s Advanced REST Client.
My authentication request was GET and it was something like this in Advanced REST:
https://username:password#URL:portnumber
its a GET request
Now when I am using Jmeter. I tried following ways:
I added HTTP Authorization Manager and mentioned Base URL and Username/password inside it.
When I am trying to do a request then its showing me “Unauthorized”
I also tried to login using normal https request but no success.
When accessed manually, a authorization popup window appears and username and password is submitted inside this window.
Please suggest me a way for how to login using Jmeter.
Few suggestions:
Most likely you have mismatch in URL you're trying hit and the one, specified in HTTP Authorization Manager, double check it.
Add View Results Tree listener and make sure that the header like:
Authorization: Basic xxxxxxxxxxxx=
is being sent along with the request and compare it with the one, sent by the real browser.
Try switching "Implementation" of your HTTP Request samplers to HttpClient3.1, the easiest way of doing this is using HTTP Request Defaults
And finally, you can use HTTP Header Manager to send the relevant header, it's name should be Authorization and value Basic and username:password encoded in Base64. There is base64Encode function available via JMeter Plugins.

Shopify webhook verification in parse

I am trying to get a shopify webhook to fill my customer class in parse.com, however something must go wrong. I don't know how to verify the parse response since Shopify sends this webhook out from it's ruby backend. I used requestbin to catch the webhook and I replicated a post request using postman to my parse url and everything works fine. Does anyone know how to debug requests like these? Is there a console in Parse where I can see all the incoming requests and the responses Parse.com sent back?
Try using Runscope for debugging webhooks. Full guide here: https://www.runscope.com/provider-guide/troubleshooting-webhooks - this is more than just a request bin. It's a full transparent proxy that will, like a bin, record the webhook notification, but will also pass it along to the intended destination (your webhook receiver) and record that response as well.

Using session token on plain HTTP Request on Parse's REST API

I am working with Parse.com REST API where my working environment restricts me from using modified header on GET requests.
For now, the GET request using plain http is served good, and we can login to the parse using url formatted like this:
https://ParseAppID:javascript-key=ParseJSKey#api.parse.com/1/login?username=someName&password=somePass
But we can't check if current session is valid, as with GET request formatted like this:
https://ParseAppID:javascript-key=ParseJSKey#api.parse.com/1/users/me
we received invalid session response
Is there any way to put session token as additional or replacement of javascript-key? Thanks

Resources