Nov 20, 2020
0
Level 1
Saves to Users table shiphero refresh token but not access token
Hello, I have one issue with shiphero graphql api. When I submit shiphero credentials, it saves to users table shiphero refresh token but not access token, although looking below in code methods apart small differences are the same. I try pass or get refresh_token in numerous ways but with no avail. ShipherroWrapperGraphQL.php
class ShipheroWrapperGraphQL
{
const AUTHENTICATION_URL = 'https://public-api.shiphero.com/auth/token';
const AUTHENTICATION_URL_REFRESH = 'https://public-api.shiphero.com/auth/refresh';
const REQUEST_URL = 'https://public-api.shiphero.com/graphql';
protected $client;
protected $user;
public function __construct(User $user)
{
$this->client = new Client();
$this->user = $user;
}
public function getAccessToken($refresh_token)
{
Log::info('[Shiphero GrapQL] Requesting Access Token');
try {
$response = $this->client->request(
'POST',
self::AUTHENTICATION_URL_REFRESH,
[
RequestOptions::JSON => [
'refresh_token' => $refresh_token,
]
]
);
} catch (\Exception $e) {
Log::error('[Shiphero GraphQL] Access Token Error ' . $e->getMessage());
return null;
}
if (empty($response)) {
return null;
}
$responseContent = $response->getBody()->getContents();
if (empty($responseContent)) {
return null;
}
$decoded = \GuzzleHttp\json_decode($responseContent, true);
$this->user->shiphero_access_token = Arr::get($decoded, 'access_token');
$this->user->shiphero_access_token = $decoded['access_token'];
$this->user->save();
return $decoded['access_token'] ?: null;
}
public function getRefreshToken($email, $password)
{
Log::info('[Shiphero GraphQL] Requesting Refresh token');
try{
$response = $this->client->request(
'POST',
self::AUTHENTICATION_URL,
[
RequestOptions::JSON => [
'username' => $email,
'password' => $password
]
]
);
} catch(\Exception $e){
Log::error('[Shiphero GraphQL] Refresh Token Error '. $e->getMessage());
return null;
}
if(empty($response)){
return null;
}
$responseContent = $response->getBody()->getContents();
if(empty($responseContent)){
return null;
}
$decoded = \GuzzleHttp\json_decode($responseContent, true);
$this->user->shiphero_refresh_token = Arr::get($decoded, 'refresh_token');
$this->user->save();
return $decoded['refresh_token'] ?: null;
}
}
HomeController.php
public function updateUser(EditUserRequest $request)
{
if(empty($request)){
return redirect('home');
}
$user = User::find(Auth::user()->id);
if(!empty($request->only('shiphero_email', 'shiphero_password'))){
$shiphero_user = new ShipheroWrapperGraphQL($user);
$shiphero_user->getRefreshToken($request->shiphero_email, $request->shiphero_password);
$shiphero_user->getAccessToken(Auth::user()->shiphero_refresh_token);
}
if(!empty($request->password)) {
$user->password = Hash::make($request->password);
}
Please or to participate in this conversation.