Skip to content

Commit 97c563f

Browse files
feat(hackathon): Get Github, Facebook and Twitter API functionality to work properly
1 parent 55f4d00 commit 97c563f

16 files changed

+498
-7
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
use App\Http\Requests;
8+
use App\Http\Controllers\Controller;
9+
use Facebook;
10+
use App\User;
11+
use Auth;
12+
use Session;
13+
use Illuminate\Support\Collection;
14+
15+
class FacebookController extends Controller
16+
{
17+
protected $user;
18+
19+
/**
20+
* Return all data to the Facebook API dashboard
21+
* @return mixed
22+
*/
23+
public function getPage()
24+
{
25+
if( Session::get('provider') !== 'facebook') {
26+
Auth::logout();
27+
28+
Session::flush();
29+
30+
return redirect('/auth/facebook');
31+
}
32+
33+
$userDetails = $this->getData();
34+
35+
return view('api.facebook')->withDetails($userDetails);
36+
}
37+
38+
/**
39+
* [getData description]
40+
* @return [type] [description]
41+
*/
42+
private function getData()
43+
{
44+
$data = Facebook::get('/me?fields=id,name,cover,email,gender,first_name,last_name,locale,timezone,link,picture', Auth::user()->getAccessToken());
45+
46+
return json_decode($data->getGraphUser(),true);
47+
}
48+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
use Auth;
8+
use Session;
9+
use App\Http\Requests;
10+
use App\Http\Controllers\Controller;
11+
use LinkedIn\LinkedIn;
12+
use GuzzleHttp\Client;
13+
14+
class LinkedInController extends Controller
15+
{
16+
/**
17+
* LinkedIn API base Url
18+
* @var string
19+
*/
20+
protected $baseUrl;
21+
22+
/**
23+
* Instance of Client
24+
* @var object
25+
*/
26+
protected $client;
27+
28+
/**
29+
* Response from requests made to LinkedIn
30+
* @var mixed
31+
*/
32+
protected $response;
33+
34+
/**
35+
* Initialize the Controller with necessary arguments
36+
*/
37+
public function __construct()
38+
{
39+
$this->baseUrl = 'https://api.linkedin.com/v1';
40+
$this->client = new Client(['base_uri' => $this->baseUrl]);
41+
}
42+
43+
/**
44+
* Set options for making the Client request
45+
* @return void
46+
*/
47+
private function setRequestOptions()
48+
{
49+
$authBearer = 'Bearer '. Auth::user()->getAccessToken();
50+
$this->client = new Client(['base_uri' => $this->baseUrl,
51+
'headers' => [
52+
'Authorization' => $authBearer,
53+
'Content-Type' => 'application/json',
54+
'Accept' => 'application/json'
55+
]]);
56+
}
57+
58+
/**
59+
* Get the response from New York times API
60+
* @param string $relativeUrl
61+
*/
62+
private function setGetResponse($relativeUrl)
63+
{
64+
$this->response = $this->client->get($this->baseUrl . $relativeUrl, []);
65+
}
66+
67+
/**
68+
* Get the whole response from a get operation
69+
* @return array
70+
*/
71+
private function getResponse()
72+
{
73+
return json_decode($this->response->getBody(), true);
74+
}
75+
76+
/**
77+
* Get the data response from a get operation
78+
* @return array
79+
*/
80+
private function getData()
81+
{
82+
return $this->getResponse();
83+
}
84+
85+
/**
86+
* Return all data to the LinkedIn API dashboard
87+
* @return mixed
88+
*/
89+
public function getPage()
90+
{
91+
if (Session::get('provider') !== 'linkedin') {
92+
Auth::logout();
93+
94+
Session::flush();
95+
96+
return redirect('/auth/linkedin');
97+
}
98+
99+
$this->setRequestOptions();
100+
101+
$relativeUrl = '/people/~:(firstName,lastName,emailAddress,pictureUrl,location,industry,numConnections,numConnectionsCapped,summary,publicProfileUrl)?format=json';
102+
103+
$this->setGetResponse($relativeUrl);
104+
105+
$userDetails = $this->getData();
106+
107+
return view('api.linkedin')->withDetails($userDetails);
108+
}
109+
}
110+

app/Http/Controllers/OauthController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Contracts\Auth\Guard;
1010
use App\Http\Controllers\Controller;
1111
use Laravel\Socialite\Contracts\Factory as Socialite;
12+
use Session;
1213

1314

1415
class OauthController extends Controller
@@ -48,19 +49,28 @@ public function findByProviderIdOrCreate($userData, $provider)
4849
{
4950
$user = User::where('provider_id', '=', $userData->id)->first();
5051

52+
Session::put('provider', $provider);
53+
5154
$email = $this->isEmailExists($userData->getEmail()) ? null : $userData->getEmail();
5255

5356
$username = $this->isUsernameExists($userData->getNickName()) ? null : $userData->getNickName();
5457

58+
$tokenSecret = property_exists($userData, "tokenSecret") ? $userData->tokenSecret : null;
59+
5560
if (empty($user)) {
61+
5662
$user = User::create([
5763
'fullname' => $userData->getName(),
5864
'username' => $username,
5965
'provider_id' => $userData->getId(),
6066
'avatar' => $userData->getAvatar(),
6167
'email' => $email,
6268
'provider' => $provider,
69+
'oauth_token' => $userData->token,
70+
'oauth_token_secret' => $tokenSecret
6371
]);
72+
73+
Session::put('provider', $provider);
6474
}
6575

6676
return $user;

app/Http/Controllers/TwitterController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace App\Http\Controllers;
44

5+
use Auth;
56
use Twitter;
7+
use Session;
68
use App\Http\Requests;
79
use Illuminate\Http\Request;
810
use App\Http\Controllers\Controller;
@@ -29,6 +31,14 @@ public function __construct()
2931
*/
3032
public function getPage()
3133
{
34+
if( Session::get('provider') !== 'twitter') {
35+
Auth::logout();
36+
37+
Session::flush();
38+
39+
return redirect('/auth/twitter');
40+
}
41+
3242
$searchedTweets = json_decode($this->searchForTweets($this->searchItem), true);
3343

3444
return view('api.twitter')->withTweets($searchedTweets['statuses']);
@@ -66,6 +76,8 @@ public function sendTweet(Request $request)
6676

6777
$tweet = $request->input('tweet') . ' #LaravelHackathonStarter';
6878

79+
Twitter::reconfig(['token' => Auth::user()->getAccessToken(), 'secret' => Auth::user()->getAccessTokenSecret()]);
80+
6981
Twitter::postTweet(['status' => $tweet, 'format' => 'json']);
7082

7183
return redirect()->back()->with('info','Your Tweet has been posted successfully');

app/Http/routes.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,18 @@
127127
'middleware' => ['auth']
128128
]);
129129

130+
Route::get('/api/facebook', [
131+
'uses' => 'FacebookController@getPage',
132+
'as' => 'api.facebook',
133+
'middleware' => ['auth']
134+
]);
135+
136+
Route::get('/api/linkedin', [
137+
'uses' => 'LinkedInController@getPage',
138+
'as' => 'api.linkedin',
139+
'middleware' => ['auth']
140+
]);
141+
130142
Route::post('/slack/message', [
131143
'uses' => 'SlackController@sendMessageToTeam',
132144
'as' => 'slack.message',

app/User.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class User extends Authenticatable
1212
* @var array
1313
*/
1414
protected $fillable = [
15-
'fullname', 'username', 'email', 'password', 'provider_id', 'provider', 'avatar', 'gender', 'location', 'website',
15+
'fullname', 'username', 'email', 'password', 'provider_id', 'provider',
16+
'avatar', 'gender', 'location', 'website', 'oauth_token', 'oauth_token_secret'
1617
];
1718

1819
/**
@@ -32,4 +33,14 @@ public function getAvatarUrl()
3233

3334
return $this->avatar;
3435
}
36+
37+
public function getAccessToken()
38+
{
39+
return $this->oauth_token;
40+
}
41+
42+
public function getAccessTokenSecret()
43+
{
44+
return $this->oauth_token_secret;
45+
}
3546
}

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"fabpot/goutte": "^3.1",
1919
"mjerwin/clockwork-sms": "^0.9.1",
2020
"lob/lob-php": "^1.6",
21-
"vluzrmos/slack-api": "^0.4.6"
21+
"vluzrmos/slack-api": "^0.4.6",
22+
"vinkla/facebook": "^2.0",
23+
"linkedinapi/linkedin": "^1.1"
2224
},
2325
"require-dev": {
2426
"fzaninotto/faker": "~1.4",

0 commit comments

Comments
 (0)