MichaelGarrez/twitch-alerts-sdk

Twitch Alerts SDK

v1.0.0 2016-01-19 16:54 UTC

This package is auto-updated.

Last update: 2024-09-12 03:30:56 UTC


README

Scrutinizer Code Quality Build Status SensioLabsInsight

简介

这个库简化了Twitch Alerts API的使用。当前版本是为与Twitch Alerts API的v1.0版本一起工作而构建的。

如何使用

首先,您需要在Twitch Alerts上注册您的应用程序,为此,请访问此URL

--> 注册您的应用程序

然后,您可以使用API包装器,并知道您的client_id、client_secret和redirect_uri参数。

Twitch API使用OAuth 2.0来处理用户身份验证。这个库可以帮助您处理身份验证过程。

用户身份验证

首先需要实例化一个Client

<?php

$client = new Client($clientId, $clientSecret, $redirectUri); // $redirectUri should also match exactly the data you entered when you registred your application

当用户接受(或拒绝)您的应用程序访问其账户时,您需要将用户重定向到Twitch Alerts。您可以请求多个权限。进一步,您将受限于您在此步骤中请求的权限。(可用权限列表在Client::SCOPES中。

<?php

$client = new Client($clientId, $clientSecret, $redirectUri);
$redirectUrl = $client->getAuthorizeUrl(['donations.create']); // You can pass multiple scopes here

// You should then need to redirect the user to this URI.
// When the user will return to your redirect_uri either the "code" parameter will be present on the query parameters (you will need it for the next step) or an "error" parameter will be present which means the user refused to allow your application to access his account.

当您从上一步获得“code”参数时,您将能够请求access_token和refresh_token。access_token是您对API进行的任何后续调用的必需品,而refresh_token是您在access_token过期时刷新access_token所需的。

生成第一个access_token和refresh_token

<?php
$client = new Client($clientId, $clientSecret, $redirectUri);
$tokens = $client->getAccessToken($code); // $code is the code returned on the previous step

// $tokens will contains multiple keys : "access_token" and "refresh_token".

现在,您可以使用其他API方法3600秒,然后您需要刷新您的access_token。

<?php
$client = new Client($clientId, $clientSecret, $redirectUri);
$tokens = $client->refreshAccessToken($refreshToken); // $refreshToken is the refreshToken you got from the previous step (or the last refreshAccessToken you did).

// $tokens will contains multiple keys : "access_token" and "refresh_token".