0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravel × Google Drive 連携 HOW TO(課金なし・読み取り専用)

0
Posted at

目的

Google Drive の 特定フォルダ内の画像を取得し、Blade で表示する

前提

  • Laravel 8〜11
  • PHP 8.x
  • 課金なし
  • 個人 or 小規模利用
  • Google Drive 読み取り専用

STEP 1. OAuth 接続の動作確認(Drive API はまだ使わない)

1. Google Cloud 側の設定

1-1. プロジェクト作成

  • Google Cloud Console
  • 新規プロジェクト作成

1-2. OAuth 同意画面

  • ユーザータイプ:外部(External)
  • 公開ステータス:テスト
  • テストユーザー:自分の Gmail を追加
  • 審査:不要(Drive read-only のみ)

1-3. OAuth クライアント ID

  • 種類:Web アプリケーション
  • 承認済みのリダイレクト URI
http://127.0.0.1:8000/auth/google/callback

※ JavaScript 生成元は不要

2. Laravel 側の準備

2-1. パッケージインストール

composer require laravel/socialite
composer require google/apiclient:^2.0

3. 環境変数(.env)

GOOGLE_CLIENT_ID=xxxxx.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=xxxxx
GOOGLE_REDIRECT_URI=http://127.0.0.1:8000/auth/google/callback

4. config/services.php

'google' => [
    'client_id' => env('GOOGLE_CLIENT_ID'),
    'client_secret' => env('GOOGLE_CLIENT_SECRET'),
    'redirect' => env('GOOGLE_REDIRECT_URI'),
],

設定後:

php artisan config:clear

5. OAuth 用 Controller(動作確認用)

namespace App\Http\Controllers;

use Laravel\Socialite\Facades\Socialite;

class GoogleAuthController extends Controller
{
    public function redirect()
    {
        return Socialite::driver('google')
            ->scopes(['https://www.googleapis.com/auth/drive.readonly'])
            ->redirect();
    }

    public function callback()
    {
        return 'callback ok';
    }
}

6. ルーティング

use App\Http\Controllers\GoogleAuthController;

Route::get('/auth/google', [GoogleAuthController::class, 'redirect']);
Route::get('/auth/google/callback', [GoogleAuthController::class, 'callback']);

7. 動作確認

  1. ブラウザでアクセス

    http://127.0.0.1:8000/auth/google
    
  2. Google ログイン & 権限許可

  3. 以下が表示されれば成功

    callback ok
    

ここまでで OAuth 接続は成功

STEP 2. 実際に Google Drive から画像を取得するための追加設定

※ ここから初めて Drive API を使う

1. Google Drive API を有効化

  • API とサービス → ライブラリ
  • Google Drive API を有効化

※ OAuth が通っていても、これが無いと取得不可

2. Drive の読み取り権限(すでに設定済み)

以下は OAuth リダイレクト時に設定する

->scopes(['https://www.googleapis.com/auth/drive.readonly'])

※ callback に書いても意味はない

3. 対象フォルダの共有設定

  • OAuth でログインする Google アカウントが

    フォルダの 閲覧権限を持っていること

4. フォルダIDを取得・設定

フォルダID取得

https://drive.google.com/drive/folders/【ここ】

.env に設定

GOOGLE_DRIVE_FOLDER_ID=xxxxxxxxxxxxxxxxxxxx

5. callback でアクセストークンを取得

public function callback()
{
    $googleUser = Socialite::driver('google')->user();
    $accessToken = $googleUser->token;

    // ここから Drive API を呼び出す
}

→ この token を使って files.list を実行する

よくハマるポイントまとめ

症状 原因
redirect_uri_mismatch Google 側と .env が完全一致していない
org_internal / ブロック OAuth が内部 or テストユーザー未登録
Socialite::user() で No message callback 以外で呼んでいる
画像が0件 フォルダID違い / 共有不足 / 直下に画像なし
403 Drive API 未有効

補足:課金について

  • Drive API read-only は 無料
  • アクセス数が増えても 課金されない
  • 制限はレート制限のみ

まとめ

  • OAuth 接続確認(callback ok)と
  • Google Drive からのファイル取得は別工程
  • Drive API 有効化・フォルダ共有・フォルダID設定を行うことで
  • 初めて画像取得が可能になる。

無事接続できたので、実際に画像取得してくぞーー

一旦ここまで、ではまた

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?