Hey? How did you accomplish this?
Using Tumblr API to post new content
I would like to use tumblr API for posting new content. I couldn't find right repository for laravel 4 to post to tumblr, so I will have to write the code.
Official API client: https://github.com/tumblr/tumblr.php it requires the AuthO
How I supposted to add this code to my project? Someone tried to post on tumblr by API in laravel?
Don't know where to start. I would like just to get some advise from more advanced coders.
For integrating an api or library such as this into Laravel.
The first step is to install the library by adding it to your composer.json.
composer require tumblr/tumblr
Next look at the documentation for how to initialize the library.
From the first line in the documentation.
// The first step is setting up a Client:
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$client->setToken($token, $tokenSecret);
You could just new up an instance of this class in your controller anytime you needed it and it would work fine but creating a Service Provider is a much better way of integrating something like this into Laravel.
You will need somewhere to store the api credentials.
Add the following to your services.php config file.
'tumblr' => [
'client_id' => env('TUMBLR_KEY'),
'client_secret' => env('TUMBLR_SECRET'),
'redirect' => env('TUMBLR_REDIRECT_URI'),
],
The redirect setting will be for oath authentication. Set it to some url that isn't used in your application. We'll create a route for it later.
Set these values in your .env file. They should match the values used when you created the app in tumblr.
Time to create the service provider.
php artisan make:provider TumblrServiceProvider
Add the following to the register method
$this->app->singleton(\Tumblr\API\Client::class, function ($app) {
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
return $client;
});
Now add the ServiceProvider to your app.php
Before you can make api calls we need an oath token and it doesn't look like the sdk has methods to create one.
Luckily there is a tumblr socialite driver.
http://socialiteproviders.github.io/providers/tumblr/
Add Socialite and the tumblr driver to your application.
composer require laravel/socialite
composer require socialiteproviders/tumblr
Then follow the instructions on the socialite driver documentation to install and configure it.
http://socialiteproviders.github.io/providers/tumblr/
Next create a route for the redirect url that you set earlier as well as for a callback after the authentication.
Route::get('login/tumblr', 'Auth\TumblrController@redirectToProvider');
Route::get('login/tumblr/callback', 'Auth\TumblrController@handleProviderCallback');
The controller might look something like this
<?php
namespace App\Http\Controllers\Auth;
use Socialite;
class TumblrController extends Controller
{
/**
* Redirect the user to the GitHub authentication page.
*
* @return Response
*/
public function redirectToProvider()
{
return Socialite::driver('tumblr')->redirect();
}
/**
* Obtain the user information from GitHub.
*
* @return Response
*/
public function handleProviderCallback()
{
$user = Socialite::driver('tumblr')->user();
$token = $user->token;
$tokenSecret = $user->tokenSecret;
// Now store these credentials somewhere as they will need to be set
// on the tumblr api client in order to make requests.
}
}
Visit the redirect url which should let you authenticate with Tumblr.
Now once you get an instance of the Tumblr\API\Client from your service provider you can call the setToken() passing it the token and secret you got from the oath callback.
With those set you should be able to make api calls with the client.
Please or to participate in this conversation.