mirror of
https://github.com/brunofontes/Memsource-API.git
synced 2025-11-17 02:30:54 -03:00
Transforming into a composer package class
As I should use this into more than one project, it would be better to turn it into a composer package. So I learn how to do that and use it at work at the same time.
This commit is contained in:
41
src/oauth.php
Normal file
41
src/oauth.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* A very compact and simple Memsource API library
|
||||
*
|
||||
* @author Bruno Fontes <developer@brunofontes.net>
|
||||
* @link https://github.com/brunofontes
|
||||
*/
|
||||
namespace BrunoFontes\Memsource;
|
||||
|
||||
class Oauth extends \BrunoFontes\Memsource
|
||||
{
|
||||
private $_url = '/oauth';
|
||||
|
||||
/**
|
||||
* Get the URL to generate the Authorization Code from Memsource
|
||||
*
|
||||
* @param string $client_id Memsource client ID
|
||||
* @param string $client_secret Memsource client secret
|
||||
* @param string $callback_uri URL that Memsource will redirect to
|
||||
*
|
||||
* @return string the authorization code
|
||||
*/
|
||||
public function getAuthorizationCodeUrl(string $client_id, string $callback_uri)
|
||||
{
|
||||
$authorize_url = $this->_url . '/authorize';
|
||||
return $authorize_url . '?response_type=code&client_id=' . $client_id . '&redirect_uri=' . $callback_uri . '&scope=openid';
|
||||
}
|
||||
|
||||
public function getAccessToken(string $authorization_code, string $client_id, string $client_secret, string $callback_uri)
|
||||
{
|
||||
$token_url = $this->_url . '/token';
|
||||
$authorization = base64_encode("$client_id:$client_secret");
|
||||
$header = ["Authorization: Basic {$authorization}", 'Content-Type: application/x-www-form-urlencoded'];
|
||||
$content = "grant_type=authorization_code&code=$authorization_code&redirect_uri=$callback_uri";
|
||||
$response = $this->post($token_url, $content, [], $header);
|
||||
if ($respose['error']) {
|
||||
throw new Exception("Error getting access token", 1);
|
||||
}
|
||||
return $response['access_token'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user