I am trying to implement BlueSnap payment(Embedded Checkout) on my WebApp in Laravel 6.
https://developers.bluesnap.com/v8976-Tools/docs/embedded-checkout.
I allowed server and public IP to access the BlueSnap Sandbox routes
First, I am getting the token from BlueSnap and this work perfect.
https://developers.bluesnap.com/v8976-Tools/docs/create-embedded-checkout-token.
But then, when I request for card transaction (https://developers.bluesnap.com/v8976-JSON/docs/auth-capture), with "pfToken" which I get from previous request, I am getting 401.
public function makePayment(Request $request) {
$endpoint = 'https://sandbox.bluesnap.com/services/2/transactions';
try {
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $endpoint, [
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Basic dXNlcm5hbWU6cGFzc3dvcmQ='
],
'json' => [
'cardTransactionType' => 'AUTH_CAPTURE',
'amount' => '25.00',
'currency' => 'USD',
'pfToken' => $request->token,
],
]);
} catch (RequestException $e) {
dd($e);
}
dd($response);
}
I also tried to send request with Postman, and I got same error.
Related
I want to send a POST request to an external API
Here is my method to call
public function recursub(Request $request) {
$users = User::where('cancel_trial', 1)->get();
foreach($users as $user) {
//dd($user->email);
$url= 'https://api.paystack.co/subscription';
$client = new Client();
$response = $client->request('POST', $url, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer sk_test_0994##############',
],
'form_params' => [
'customer' => '233########',
'plan' => 'PLN_###########',
]
]);
dd($response->getBody());
//dd($response->getBody()->getContents());
}
}
But I keep getting this error when I call the endpoint
GuzzleHttp \ Exception \ ClientException (400)
Client error: POST https://api.paystack.co/subscription resulted in a 400 Bad Request response: { "status": false, "message": "Request body could not be parsed. Make sure request body matches specified content-ty (truncated...)
It's looks like that you send wrong request to API.
If you set application/json maybe you should pass parameter in body, not in form params:
$response = $client->request('POST', $url, [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer sk_test_0994##############',
],
'body' => json_encode([
'customer' => '233########',
'plan' => 'PLN_###########',
])
]);
Another information that maybe can be helpful is that you can disable exception with ['http_errors' => false] option (http://docs.guzzlephp.org/en/stable/request-options.html#http-errors).
Package used is the : https://github.com/coderello/laravel-passport-social-grant
I have followed all the expected changed with the tutorial provided at :
https://medium.com/#hivokas/api-authentication-via-social-networks-for-your-laravel-application-d81cfc185e60
url :
http://myurl.com/oauth/token
output:
{
"error": "invalid_credentials",
"error_description": "The user credentials were incorrect.",
"message": "The user credentials were incorrect."
}
parameter have used with the postman is as followed:
grant_type' => 'social', // static 'social' value
'client_id' => $clientId, // client id
'client_secret' => $clientSecret, // client secret
'provider' => $providerName, // name of provider (e.g., 'facebook', 'google' etc.)
'access_token' => $providerAccessToken, // access token issued by specified provider
],
what can be the issue here ?
For Facebook you need to have the client_id and secret, in your services.php
Code inside FacebookProvider of Socialite
protected function getUserByToken($token)
{
$this->lastToken = $token;
$params = [
'access_token' => $token,
'fields' => implode(',', $this->fields),
];
if (! empty($this->clientSecret)) {
$params['appsecret_proof'] = hash_hmac('sha256', $token, $this->clientSecret);
}
$response = $this->getHttpClient()->get($this->graphUrl.'/'.$this->version.'/me', [
'headers' => [
'Accept' => 'application/json',
],
'query' => $params,
]);
return json_decode($response->getBody(), true);
}
So, google kinda works without secret.
I am having trouble getting a laravel application to access an api endpoint in another laravel app, same server. I can get it to work in terminal, and postman but not from the laravel app itself, both getting an auth token and using work in p;ostman and terminal but not in laravel. (killing me that this was working the other day)
I have tried many Guzzle and curl configs, Using WAMP on Win 10. Laravel Framework version 5.3.31, PHP 7.2.18
try {
$response = $http->request('POST',$this->base_oauth_url. 'oauth/token', [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'form_params' => [
'grant_type' => 'password',
'client_id' => self::API_CLIENT_ID,
'client_secret' => self::API_SECRET,
'username' => self::API_USERNAME,
'password' => self::API_PASSWORD,
'scope' => '*',
],
'debug'=>$this->debug,
'allow_redirects' => false,
'verify' => false,
]);
} catch (ClientException $e) {
throw new Exception("Guzzle Error: {$e->getMessage()}");
} catch (Exception $e) {
throw new Exception("Send Error: ({$e->getCode()}): {$e->getMessage()}");
}
$response = json_decode((string) $response->getBody()->getContents());
$data = array();
$data['access_token'] = $response->access_token;
$data['refresh_token'] = $response->refresh_token;
Error:
Guzzle Error: Client error: POST http://mytestdomain.com/oauth/token resulted in a 401 Unauthorized response:
{"error":"invalid_client","message":"Client authentication failed"}
Greetings and Regards
I am designing an api with Laravel and using a passport for an app.
I'm having trouble logging in using the login endpoint.
The problem I encountered is as follows:
I will send the necessary parameters to the server including
client_id - grant_type - username - password - client_secret - scope:
{{server_url}}/api / v1 / login
In the login method:
After evaluating entries I send a request to oauth/token to receive theta token.
I get the error below.
GuzzleHttp\Exception\ClientException: Client error: POST
http://divar.local/oauth/token resulted in a 400 Bad Request
response: {"error":"invalid_grant","error_description":"The provided
authorization grant (e.g., authorization code, resource owner
(truncated...)
The contents of the login method
public function login()
{
$validator = Validator::make(request()->all(), [
'mobile_number' => 'required',
'password' => 'required',
]);
if ($validator->fails()) {
$response['errors'] = $validator->errors();
return response()->json($response, 401);
}
$clientOauthData = Client::where('password_client', 1)->where('name', 'Laravel Password Grant Client')->first();
$http = new \GuzzleHttp\Client;
$response = $http->post(env('APP_URL') . '/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $clientOauthData->id,
'client_secret' => $clientOauthData->secret,
'username' => request('mobile_number'),
'password' => request('password'),
'scope' => '',
],
'headers' => [
'Accept' => 'application/json'
]
]);
$response = json_decode((string)$response->getBody(), true);
return response()->json($response, 200);
}
Request headers :
Request body :
This is how I always proceed this request.
public function login(Request $request)
{
$this->validate($request, [
'email' => 'required|mobile_phone',
'password' => 'required|string',
]);
$http = new \GuzzleHttp\Client;
try {
$response = $http->post(config('services.passport.login_endpoint'), [
'form_params' => [
'grant_type' => 'password',
'client_id' => 'your client ID',
'client_secret' => 'secret ID,
'username' => $request->mobile_phone,
'password' => $request->password,
// 'scope' => '',
],
]);
return $response->getBody();
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
if ($e->getCode() == 401) {
return response()->json(['message' => 'This action can\'t be perfomed at this time. Please try later.'], $e->getCode());
} else if ($e->getCode() == 400) {
return response()->json(['message' => 'These credentials do not match our records.'], $e->getCode());
}
return response()->json('Something went wrong on the server. Please try letar.', $e->getCode());
}
}
I need to integrate an API so I write function:
public function test() {
$client = new GuzzleHttp\Client();
try {
$res = $client->post('http://example.co.uk/auth/token', [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'json' => [
'cliend_id' => 'SOMEID',
'client_secret' => '9999jjjj67Y0LBLq8CbftgfdreehYEI=',
'grant_type' => 'client_credentials'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
dd($res);
}
catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
}
}
as a responde I got message:
{"data":{"error":"invalid_clientId","error_description":"ClientId should be sent."}}
Now when I try to run the same url with same data in POSTMAN app then I get correct results:
What is bad in my code? I send correct form_params also I try to change form_params to json but again I got the same error...
How to solve my problem?
The problem is that in Postman you're sending the data as a form, but in Guzzle you're passing the data in the 'json' key of the options array.
I bet that if you would switch the 'json' to 'form_params' you would get the result you're looking for.
$res = $client->post('http://example.co.uk/auth/token', [
'form_params' => [
'client_id' => 'SOMEID',
'client_secret' => '9999jjjj67Y0LBLq8CbftgfdreehYEI=',
'grant_type' => 'client_credentials'
]
]);
Here's a link to the docs in question: http://docs.guzzlephp.org/en/stable/quickstart.html#sending-form-fields
Also, I noticed a typo - you have cliend_id instead of client_id.