So today i tried to “connect” to twitch api with php but now im stuck on that authorization token is in the url fragment and i dont know how to get it
URL :
use NewTwitchApi\HelixGuzzleClient;
use NewTwitchApi\NewTwitchApi;
require 'vendor/autoload.php';
include_once('lib/urlGen.php');
include_once('lib/Config.php');
$urlGen = new urlGen();
echo '<a href="'.$urlGen->generateURL().'">Authenticate Me</a><br/>';
if(isset($_GET['error'])){
echo "error: " . $_GET['error'] . "<br>";
if(isset($_GET['error_description'])){
echo "error description: " . $_GET['error_description'] . "<br>";
}
}
if(!isset($_GET['access_token'])){
echo "code not set" . "<br>";
return;
}
$conf = new Config();
$accessToken = $_GET['access_token'];
var_dump($accessToken);
// The Guzzle client used can be the included `HelixGuzzleClient` class, for convenience.
// You can also use a mock, fake, or other double for testing, of course.
$helixGuzzleClient = new HelixGuzzleClient($conf->getClientId());
// Instantiate twitchApi. Can be done in a service layer and injected as well.
$newTwitchApi = new NewTwitchApi($helixGuzzleClient, $conf->getClientId(), $conf->getClientSecret());
try {
// Make the API call. A ResponseInterface object is returned.
$response = $newTwitchApi->getUsersApi()->getUserByAccessToken($accessToken);
// Get and decode the actual content sent by Twitch.
$responseContent = json_decode($response->getBody()->getContents());
// Return the first (or only) user.
$user = $responseContent->data[0];
} catch (GuzzleException $e) {
// Handle error appropriately for your application
exit("error occured");
}
print_r($user);
For PHP you really should not be using implicit auth. As a “server” cannot see the URL Fragment. Implicit auth is intended to be used for apps that don’t have a server.
Ok thanks I got it working somehow but now I want to ask if I can reuse the south token or the authorization code so the user can walk through the process just once
The ?code= is valid for one exchange to an access token.
Then you may store and use the access token (and it’s refresh token)
You can use the refresh token to generate a new token if/when the access token expires.
nvm i run into another problem the api im using doesnt support getting subscribers so how can i get the data from user acces token(or how to do the request) and how can i check if user has sub somewhere
To get subscriptions status, requires an oAuth token from the broadcaster, with the relevant scopes attached
So if you want to see if I am subscribed to CohhCarnage and/or Lirik, you need
A token from CohhCarnage
A token from Lirik
My userID, (usually got from asking me to login to your site)
So you need user access tokens from the broadcaster you want to lookup against. A “privileged” access token is generated the same way as you would a “non privileged” token.
what is the broadcaster id is it the id i get with email name etc.
2.when im getting subscribers max number in request is 100 so i need to send multiple requests?
You can use the token with the users endpoint to get the users ID. A user is also a broadcaster. different works are used to describe with endpoints that deal with more than one users
Yes, load a page, get the cursor, load the next page. Repeat till no more cursor
Is it somehow possible to get the “sub streak”(the think you got next to name in chat) i want to filter only subscribers with something like 2 months streak for example
So If I understand it correctly someone buys sub somewhere on 1.1 the he will pass one month and sub again on 3.3 and that’s the date I’ve got and it will be the same till another resub