Code Monkey home page Code Monkey logo

laragab's Introduction

Packagist

LaraGab - Gab Laravel API package Gab

Gab API for Laravel 5

Before you can utilise this API you need to have an developer account (which requires a Pro account), click here to go to your settings.

Installation

First add bishopj88/laragab to composer.json.

"bishopj88/laragab": "dev-master"

Run composer update to pull down the latest version of LaraGab. Or run

composer require bishopj88/laragab

Now open up /config/app.php and add the service provider to your providers array.

'providers' => [
	BishopJ88\LaraGab\LaraGabServiceProvider::class,
]

Now add the alias.

'aliases' => [
	'LaraGab' => BishopJ88\LaraGab\Facades\LaraGabFacade::class,
]

LaraGab configuration

Run php artisan vendor:publish --provider="BishopJ88\LaraGab\LaraGabServiceProvider" and modify the config file with your own information.

/config/laragab.php

Or even better, just add the following to your .env file and you'll be on your way:

LARAGAB_CLIENTID=
LARAGAB_SECRET=
LARAGAB_REDIRECT_URI=
LARAGAB_SCOPE="read engage-user engage-post write-post notifications"

LARAGAB_TOKEN_TYPE=
LARAGAB_EXPIRES_IN=
LARAGAB_ACCESS_TOKEN=
LARAGAB_REFRESH_TOKEN=

Methods

Connection

  • generateLoginURL( $client_id, $redirect_uri, $scope, $html = 0) - Generates a URL that can be used to authenticate.
  • get_access_token( $code ) - Retrieving the access tokens.

Helpers

  • outputAsArray( $output = false ) - Allows the user to work with the output as an array.

Engage Posts

  • postUpvote( ['postID' => ''] ) - Upvotes given post.
  • removeUpvote( ['postID' => ''] ) - Removes the upvote for given post.
  • removeDownvote( ['postID' => ''] ) - Removes the downvote for given post.
  • postDownvote( ['postID' => ''] ) - Downvotes given post.
  • postRepost( ['postID' => ''] ) - Reposts given post.
  • removeRepost( ['postID' => ''] ) - Remove repost record for given post.
  • getPostDetails( ['postID' => ''] ) - Returns the details of given post.

Engage Users

  • unfollowUser( ['username' => ''] ) - Unfollows given user.
  • followUser( ['username' => ''] ) - Follows given user or creates a follow request if the target user is private.

Notifications

  • getNotifications( ['before' => 0] ) - Returns latest notifications.

Read

  • getMe() - Returns the information about logged-in user.
  • getUser( ['username' => ''] ) - Returns the information about a user with given username.
  • getUserFollowers( ['username' => '', 'before' => 0] ) - Returns followers of given user.
  • getUserFollowing( ['username' => '', 'before' => 0] ) - Returns the users that given user is following.
  • getFeed( ['before' => ''] ) - Returns the main feed of the authenticated user.
  • getUserFeed( ['username' => '', 'before' => 0] ) - Returns the feed of given user.
  • getPopularFeed() - Returns the popular feed.
  • getPopularUsers() - Returns popular users.
  • getPopularGroups() - Returns a list of groups with more activities recently.
  • getGroupDetails( ['group_id' => ''] ) - Returns details of given group.
  • getGroupUsers( ['group_id' => '', 'before' => 0] ) - Returns a list of given group's members.
  • getGroupModerationLogs( ['group_id' => ''] ) - Returns a list of given group's moderation logs.

Write Post

  • createPost( ['body' => 'test'] ) - Creates a post. Check here for all options.
  • createMediaAttachment( ['file' => 'File URL'] ) - Creates a media attachment with given image.

Authentication Example

First create a link allowing you to authorize the app with your Gab account (you could technically also redirect it directly using the 'html => 0' parameter):

Route::get('/gab/login', function()
{
    $gab = new \LaraGab;
    echo $gab::generateLoginURL(
        Config::get('laragab.gab_clientID'),
        Config::get('laragab.gab_redirect_uri'),
        Config::get('laragab.gab_scope'),
        1);
});

Secondly, create a route for the callback. This should be the same as your redirect_URI in your config:

Route::get('/gab/callback', function()
{
    $code = Request::query('code');
    
    $gab = new \LaraGab;
    echo $gab::get_access_token( $code );
});

When this step is succesful, you'll receive your tokens (in JSON format). You could write something that saves it to a file, database or cache. But I decided not to include this so you have maximum flexibility what you want to do next.

After this, when you've saved your tokens and hooked them up to your .env or config file, now the fun can begin:

Route::get('/gab/fun', function()
{
    $gab = new \LaraGab;
    // Shows Andrew's account details
    echo $gab::getUser(['username' => 'a']);
    // Returns a media attachment (only images are allow... GIFs up to 4MB)
    echo $gab::createMediaAttachment(['file' => public_path() . '/photo_2018-07-30_21-03-33.jpg']);
    // Returns your own account details
    echo $gab::getMe();
});

After finding a flaw with multiple image uploads, I've fixed it and provided a working example:

Route::get('/gab/multiple-image', function()
{
    $pictures = ['image1.jpg', 'image2.jpg'];
    $uploaded = [];
    
    $gab = new \LaraGab;
    
    foreach( $pictures as $picture ){
        try{
            array_push($uploaded, $gab::createMediaAttachment( ['file' => $picture] ) );
        } catch (\Exception $e) {
            dd($e);
        }
    }
    
    $post = [
        'body' => 'Testing multiple file uploads',
        'media_attachments[]' => $uploaded
    ];
    
    $gab::createPost( $post );
});

License

The MIT License (MIT). Please see License File for more information.

laragab's People

Contributors

bishopj88 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

mikecarcara

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.