dev-master
9999999-devgoogle in laravel
MIT
The Requires
by hoanghiep
Wallogit.com
2017 © Pedro Peláez
google in laravel
Vào ứng dụng laravel vào file composer.json thêm dòng :, (*1)
"minimum-stability": "dev",, (*2)
Để sửa lỗi, (*3)
at any version for your minimum-stability (stable). Check the package spelling or your minimum-stability, (*4)
sau đó mở composer chạy lệnh tải gói, (*5)
composer require hoanghiep/laravel-google-api, (*6)
Sau khi tải gói lưu ý chuẩn bị :, (*7)
Sửa lỗi cURL error 60: SSL :, (*8)
tải file https://curl.haxx.se/ca/cacert.pem, (*9)
để vào thư mục php của bạn, (*10)
sau đó sửa file php.ini, (*11)
tìm dòng ;curl.cainfo sửa thành, (*12)
curl.cainfo= "path/cacert.pem", (*13)
Lưu ý các phiên bản sử dụng có thể ko sửa được do phiên bản apache ví dụ dùng wampp 5.5.12 ko được nhưng bản 5.6.12 lại được., (*14)
cấu hình cơ sở dữ liệu trong tập tin .env, (*15)
Hoanghiep\Googleapi\GoogleClientProvider::class, (*16)
php artisan vendor:publish, (*17)
Các file được tạo ra gồm, (*18)
php artisan migrate, (*19)
localhost/project-name/social/google/redirect, (*20)
NotFoundHttpException in RouteCollection.php line 161:, (*21)
sửa khai báo url như sau :, (*22)
vào app/Http/routes.php, (*23)
thêm định nghĩa url /test gọi đến UserinfoController@index, (*24)
Route::get('/test',["middleware"=>"web","uses"=>"Hoanghiep\UserinfoController@index"]);, (*25)
nếu ko dùng user /test định nghĩa trong file config/google redirectPath bạn dùng home chẳng hạn thì nó sẽ như này, (*26)
Route::get('/home',["middleware"=>"web","uses"=>"Hoanghiep\UserinfoController@index"]);, (*27)
use \Hoanghiep\Googleapi\hoanghiep\GoogleController;, (*28)
// ke thua lop xu ly class UserinfoController extends GoogleController {, (*29)
}, (*30)
$client = App::make("Google_Client");, (*31)
$this->handleProvider($request);, (*32)
getToken "lấy token và số lần làm mới thẻ", (*33)
$token = $this->client_array;, (*34)
Check Token, (*35)
if (!isset($token[0])): return redirect()->route("google.redirect"); endif;, (*36)
getAccessToken "lấy thẻ truy cập" $accessToken = $token[1];, (*37)
setAccessToken "Sử dụng thẻ truy cập" $client->setAccessToken($accessToken);, (*38)
gọi api đã khai báo trong và truyền vào $client hiện tại $google_oauth = App::make("Google_Service_Oauth2", [$client]); $user = $google_oauth->userinfo->get();, (*39)
hiển thị kết quả, (*40)
dd($user);, (*41)
Vào config/app.php thêm provider, (*42)
App\Providers\GoogleServiceProvider::class,, (*43)
Thêm các class dịch vụ cần sử dụng ví dụ // ở trên class use Google_Service_Youtube; // thêm một class mới vào container service laravel trong method register(){, (*44)
$this->app->bind("Google_Service_Youtube", function ($app, array $client) { return new Google_Service_Youtube($client[0]); });, (*45)
}, (*46)
Cấu hình thêm ủy quyền mới phạm vi truy cập dữ liệu trong config/google.php chú ý bật các Api cần sử dụng, (*47)
Route::get('social/google/youtube',["middleware"=>"web","uses"=>"Hoanghiep\YoutubeController@index"]);, (*48)
php artisan make:controller Hoanghiep\YoutubeController, (*49)
trông như thế này :, (*50)
<?php, (*51)
namespace App\Http\Controllers\Hoanghiep;, (*52)
use Illuminate\Http\Request; use App\Http\Requests; use App; use \Hoanghiep\Googleapi\Hoanghiep\GoogleController;, (*53)
class YoutubeController extends GoogleController { // code }, (*54)
trong phần code có các tác vụ gọi tới máy chủ lấy thẻ ủy quyền và sử dụng thẻ ủy quyền tương tự., (*55)
public function index(Request $request) { $client = App::make("Google_Client"); $this->handleProvider($request); $token = $this->client_array; if (!isset($token[0])): return redirect()->route("google.redirect"); endif; $number_refresh = $token[0]; if ($number_refresh == 1): return redirect()->route("google.redirect"); endif; $accessToken = $token[1]; $client->setAccessToken($accessToken);, (*56)
$youtube = App::make("Google_Service_Youtube",[$client]);
dd($youtube);
}, (*57)
http://localhost/{project-name}/social/google/youtube, (*58)
Kết quả :, (*59)
Google_Service_YouTube {#193 ▼ +activities: Google_Service_YouTube_Resource_Activities {#187 ▶} +captions: Google_Service_YouTube_Resource_Captions {#192 ▶} +channelBanners: Google_Service_YouTube_Resource_ChannelBanners {#182 ▶} +channelSections: Google_Service_YouTube_Resource_ChannelSections {#190 ▶} +channels: Google_Service_YouTube_Resource_Channels {#213 ▶} +commentThreads: Google_Service_YouTube_Resource_CommentThreads {#212 ▶} +comments: Google_Service_YouTube_Resource_Comments {#211 ▶} +fanFundingEvents: Google_Service_YouTube_Resource_FanFundingEvents {#210 ▶} +guideCategories: Google_Service_YouTube_Resource_GuideCategories {#200 ▶} +i18nLanguages: Google_Service_YouTube_Resource_I18nLanguages {#199 ▶} +i18nRegions: Google_Service_YouTube_Resource_I18nRegions {#198 ▶} +liveBroadcasts: Google_Service_YouTube_Resource_LiveBroadcasts {#196 ▶} +liveChatBans: Google_Service_YouTube_Resource_LiveChatBans {#197 ▶} +liveChatMessages: Google_Service_YouTube_Resource_LiveChatMessages {#194 ▶} +liveChatModerators: Google_Service_YouTube_Resource_LiveChatModerators {#195 ▶} +liveStreams: Google_Service_YouTube_Resource_LiveStreams {#204 ▶} +playlistItems: Google_Service_YouTube_Resource_PlaylistItems {#203 ▶} +playlists: Google_Service_YouTube_Resource_Playlists {#202 ▶} +search: Google_Service_YouTube_Resource_Search {#201 ▶} +sponsors: Google_Service_YouTube_Resource_Sponsors {#205 ▶} +subscriptions: Google_Service_YouTube_Resource_Subscriptions {#188 ▶} +thumbnails: Google_Service_YouTube_Resource_Thumbnails {#189 ▶} +videoAbuseReportReasons: Google_Service_YouTube_Resource_VideoAbuseReportReasons {#181 ▶} +videoCategories: Google_Service_YouTube_Resource_VideoCategories {#216 ▶} +videos: Google_Service_YouTube_Resource_Videos {#206 ▶} +watermarks: Google_Service_YouTube_Resource_Watermarks {#218 ▶} +batchPath: null +rootUrl: "https://www.googleapis.com/" +version: "v3" +servicePath: "youtube/v3/" +availableScopes: null +resource: null -client: Google_Client {#179 ▶} +"serviceName": "youtube" }, (*60)
google in laravel
MIT