Skip to content

Commit

Permalink
Commit for the first release of the oauth2-twitch package.
Browse files Browse the repository at this point in the history
  • Loading branch information
tpavlek committed Feb 2, 2015
0 parents commit 6ce0f1d
Show file tree
Hide file tree
Showing 7 changed files with 456 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
vendor/
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Twitch provider for league/oauth2-client
=========================================

This is a package to integrate Battle.net authentication with the [OAuth2 client library](https://github.com/thephpleague/oauth2-client) by
[The League of Extraordinary Packages](http://thephpleague.com).

To install, use composer:

```bash
composer require depotwarehouse/oauth2-twitch
```

Usage is the same as the league's OAuth client, using `\Depotwarehouse\OAuth2\Client\Twitch\Provider\Twitch` as the provider.
For example:

```php
$provider = new \Depotwarehouse\OAuth2\Client\Twitch\Provider\Twitch([
'clientId' => "YOUR_CLIENT_ID",
'clientSecret' => "YOUR_CLIENT_SECRET",
'redirectUri' => "http://your-redirect-uri"
]);
```

You can also optionally add a `scopes` key to the array passed to the constructor. The available scopes are documented
on the [Twitch API Documentation](https://github.com/justintv/Twitch-API/blob/master/authentication.md).

> Note: The provider uses the "user_read" scope by default. If you pass other scopes, and want the ->getUserDetails() method
to work, you will need to ensure the "user_read" scope is used.

```php
if (isset($_GET['code']) && $_GET['code']) {
$token = $this->provider->getAccessToken("authorizaton_code", [
'code' => $_GET['code']
]);

// Returns an instance of Depotwarehouse\OAuth2\Client\Twitch\Entity\TwitchUser
$user = $this->provider->getUserDetails($token);

$user->getDisplayName();
$user->getId()
$user->getType();
$user->getBio();
$user->getEmail();
$user->getPartnered();
```
18 changes: 18 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "depotwarehouse/oauth2-twitch",
"description": "A Twitch provider for league/oauth2-client",
"authors": [
{
"name": "Troy Pavlek",
"email": "troy@depotwarehouse.net"
}
],
"require": {
"league/oauth2-client": "0.7.*"
},
"autoload": {
"psr-4": {
"Depotwarehouse\\OAuth2\\Client\\Twitch\\": "src/"
}
}
}
227 changes: 227 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

require 'vendor/autoload.php';

$provider = new \Depotwarehouse\OAuth2\Client\Twitch\Provider\Twitch([
'clientId' => "bqzmhb7560sdxx7ixll05hmo53taxwp",
'clientSecret' => "l8z2b6c0ov025fhg1s14v3eosfdut4r",
'redirectUri' => "http://localhost:8000"
]);

if (isset($_GET['code']) && $_GET['code']) {
$token = $provider->getAccessToken("authorization_code", [
'code' => $_GET['code']
]);

$user = $provider->getUserDetails($token);
var_dump($user);


} else {
header('Location: ' . $provider->getAuthorizationUrl());
}
78 changes: 78 additions & 0 deletions src/Entity/TwitchUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Depotwarehouse\OAuth2\Client\Twitch\Entity;

class TwitchUser
{

/** @var string */
protected $display_name;
/** @var int */
protected $id;
/** @var string */
protected $type;
/** @var string */
protected $bio;
/** @var string */
protected $email;
/** @var bool */
protected $partnered;

public function __construct(array $attributes = array()) {
$this->id = $attributes['_id'];
$this->display_name = $attributes['display_name'];
$this->type = $attributes['type'];
$this->bio = $attributes['bio'];
$this->email = $attributes['email'];
$this->partnered = $attributes['partnered'];
}

/**
* @return string
*/
public function getDisplayName()
{
return $this->display_name;
}

/**
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* @return string
*/
public function getType()
{
return $this->type;
}

/**
* @return string
*/
public function getBio()
{
return $this->bio;
}

/**
* @return string
*/
public function getEmail()
{
return $this->email;
}

/**
* @return boolean
*/
public function isPartnered()
{
return $this->partnered;
}

}
Loading

0 comments on commit 6ce0f1d

Please sign in to comment.