Created
February 21, 2018 13:31
-
-
Save LennardWesterveld/722d3d80a10182609d5362c1d417e43a to your computer and use it in GitHub Desktop.
InstagramClient Drupal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\ko_social_feed_instagram; | |
use Drupal\Core\Url; | |
use Symfony\Component\HttpFoundation\RedirectResponse; | |
use Vinkla\Instagram\InstagramException; | |
/** | |
* {@inheritdoc} | |
*/ | |
class InstagramClient { | |
const INSTAGRAM_API_URL = 'https://api.instagram.com/'; | |
/** | |
* The access token. | |
* | |
* @var string | |
*/ | |
protected $accessToken; | |
/** | |
* The http client. | |
* | |
* @var \GuzzleHttp\Client | |
*/ | |
protected $httpClient; | |
/** | |
* Create a new Instagram instance. | |
* | |
* @param string $accessToken | |
* Instagram accessToken. | |
*/ | |
public function __construct(string $accessToken) { | |
$this->accessToken = $accessToken; | |
$this->httpClient = \Drupal::httpClient(); | |
} | |
/** | |
* Fetch the media items. | |
* | |
* @throws \Vinkla\Instagram\InstagramException | |
* | |
* @return array | |
* return response. | |
*/ | |
public function getRecentMedia($max_id = NULL, $min_id = NULL, $count = NULL): array { | |
$response = $this->httpClient->get(self::INSTAGRAM_API_URL . 'v1/users/self/media/recent', | |
[ | |
'query' => | |
[ | |
'access_token' => $this->accessToken, | |
// Is not working for now. | |
// TODO: fix later on when have clearns from Instagram. | |
// 'max_id' => $max_id, | |
// 'min_id' => $min_id,. | |
'count' => $count, | |
], | |
]); | |
if ($response->getStatusCode() === 400) { | |
$body = json_decode((string) $response->getBody()); | |
throw new InstagramException($body->meta->error_message); | |
} | |
return json_decode((string) $response->getBody())->data; | |
} | |
/** | |
* Start auth code request / redirect. | |
* | |
* @param string $client_id | |
* Client id. | |
* @param string $redirect_uri | |
* Valid Instagram redirect uri. | |
*/ | |
public static function authenticate($client_id, $redirect_uri) { | |
$url = Url::fromUri(self::INSTAGRAM_API_URL . 'oauth/authorize', [ | |
'query' => [ | |
'client_id' => $client_id, | |
'response_type' => 'code', | |
'redirect_uri' => $redirect_uri, | |
], | |
]); | |
$response = new RedirectResponse($url->toString(), 302); | |
$response->send(); | |
} | |
/** | |
* Retrieve access token. | |
* | |
* @param string $code | |
* Code from authenticate call. | |
* @param string $client_id | |
* Client id. | |
* @param string $client_secret | |
* Client secret. | |
* @param string $redirect_uri | |
* Valid Instagram redirect uri. | |
* | |
* @return array|object | |
* Response. | |
*/ | |
public static function retrieveAccessToken($code, $client_id, $client_secret, $redirect_uri) { | |
$client = \Drupal::httpClient(); | |
$response = $client->post(self::INSTAGRAM_API_URL . 'oauth/access_token', [ | |
'form_params' => [ | |
'client_id' => $client_id, | |
'client_secret' => $client_secret, | |
'grant_type' => 'authorization_code', | |
'redirect_uri' => $redirect_uri, | |
'code' => $code, | |
], | |
]); | |
return json_decode($response->getBody()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to get big size images use the following: