Giter Club home page Giter Club logo

oauth2-reddit's Introduction

Reddit OAuth2 Provider

Build Status Scrutinizer Scrutinizer Coverage Latest Version License Join the chat at https://gitter.im/rtheunissen/oauth2-reddit

This package provides Reddit integration for thephpleague/oauth2-client.

Installation

composer require rtheunissen/oauth2-reddit

Usage

use Rudolf\OAuth2\Client\Provider\Reddit;

$reddit = new Reddit([
    'clientId'      => 'yourClientId',
    'clientSecret'  => 'yourClientSecret',
    'redirectUri'   => 'yourRedirectUri',
    'userAgent'     => 'platform:appid:version, (by /u/username)',
    'scopes'        => ['identity', 'read', ...],
]);

Requesting an access token

There are four different ways to request an access token, and you should be able to determine which to use based on the nature of your application.

Have a read through the Reddit OAuth2 Wiki to find out more.

For web apps, using 'code' and 'state'
$url = $reddit->getAuthorizationUrl([
    'duration' => $duration,  // "permanent" or "temporary" by default
]);

You'll receive both code and state when redirected from Reddit.

$accessToken = $reddit->getAccessToken('authorization_code', [
    'code'  => $code,
    'state' => $state
]);
For scripts intended for personal use, using 'username' and 'password'
$accessToken = $reddit->getAccessToken('password', [
    'username' => $username,
    'password' => $password,
]);
For installed applications

You should generate and save unique ID on your client. The ID should be unique per-device or per-user of your app. A randomized or pseudo-randomized value is acceptable for generating the ID; however, you should retain and re-use the same device_id when renewing your access token.

$accessToken = $reddit->getAccessToken('installed_client', [
    'device_id' => $deviceId,  // 20-30 character ASCII string
]);
For confidential clients (web apps / scripts)
$accessToken = $reddit->getAccessToken('client_credentials');

Refreshing an access token

The only way to get a refresh token is by using the state and code redirect flow, with the duration set as "permanent". The resulting access token will have a valid refreshToken property, which you can use to refresh the token.

Note that the refreshed token won't have a refreshToken field. You should use the same refresh token every time you refresh the current token, and simply update its accessToken and expires properties.

$refreshToken = $reddit->getAccessToken('refresh_token', [
    'refresh_token' => $accessToken->refreshToken
]);

$accessToken->accessToken = $refreshToken->accessToken;
$accessToken->expires = $refreshToken->expires;

// Remember to re-store the refreshed access token at this point

Using the access token

Reddit requires a few authorization headers when making authenticated API requests. These can be accessed using $reddit->getHeaders($token).

Note: The pending v1.0.0 release of thephpleague/oauth2-client will make this easier by providing an authenticated request object which you can adjust for each request.

Until then, you are advised to use either a dedicated HTTP client or the client used by the provider:

$client = $reddit->getHttpClient(); // Guzzle 3

oauth2-reddit's People

Contributors

gitter-badger avatar rtheunissen avatar skillfish avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

oauth2-reddit's Issues

Scopes issue

Hello!

First let me just say thank you for creating this amazing library, now I have been testing some things and I needed to set de 'save' scope and I went on and did this:

$reddit = new Reddit([
   . . .
    'scopes'        => ['identity', 'read', 'save'],
]);

But this does not add the save scope, I went on and looking to the code I can see that the variable its not call scopes but scope and you need to add it when you are calling the method to get the authorizationURL, like this:

$reddit->client->getAuthorizationUrl([
            'duration' => 'permanent',  // "permanent" or "temporary" by default
            'scope'        => ['identity', 'read', 'save'],
        ]); 

Did I make something wrong? Is this the intended behaviour? Should I make a merge request to update the documentation or maybe Should I try to fix it on the code itself so the code works just like explained on the docs?

Thanks again

Setting duration option to permanent results in an error

When setting the duration parameter like so:

$url = $reddit->getAuthorizationUrl([
            'duration' => 'permanent'
        ]);

The generated URL has duplicate duration parameters:
https://ssl.reddit.com/api/v1/authorize?duration=permanent&state=<scrubbed>&scope=identity%2Cread&response_type=code&approval_prompt=auto&redirect_uri=https%3A%2F%2Flocal.test%2Freddit%2Fauthorize&client_id=<scrubbed>&duration=permanent

This results in the following error when the user is redirected to Reddit:

bad request
request contains duplicate parameter for oauth2: duration

Use league/oauth2-client ~1.0@dev

The other php league oauth2 clients have move onto using ~1.0@dev as a dependency. Unable to update the other oauth2 clients because this client uses league/oauth2-client : 1.12.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.