> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# OAuth 2.0 認証 - Google Sheets API for Laravel

> Google Sheets API の OAuth 2.0 認証。ユーザー個別アクセス向け。

OAuth 2.0 認証ではユーザーが個別の Google Sheets にアクセス権を付与します。ユーザー個別のデータを扱うアプリケーション向けです。

## 適用場面

* **ユーザー中心のアプリ** — ユーザーが自分の Google Sheets にアクセス
* **マルチテナントアプリ** — 異なるユーザーが異なるスプレッドシートを管理
* **個人データアクセス** — ユーザー個別アカウントのシートを読み書き
* **デスクトップ・Web アプリ** — ユーザーインタラクションが可能

## 前提条件

* Google Cloud Console プロジェクト
* Google Sheets API と Google Drive API を有効化
* Laravel Socialite（推奨）

## セットアップ

<Steps>
  <Step title="Google Cloud Console を設定">
    1. [Google Cloud Console](https://console.cloud.google.com/) にアクセス
    2. プロジェクトを選択または新規作成
    3. **APIs & Services** > **Library** に移動
    4. 以下の API を有効化：
       * **Google Sheets API**
       * **Google Drive API**
  </Step>

  <Step title="OAuth 2.0 認証情報を作成">
    1. **APIs & Services** > **Credentials** に移動
    2. **Create Credentials** > **OAuth client ID** をクリック
    3. OAuth 同意画面を設定（初回のみ）：
       * ユーザータイプを **External** に選択
       * 必須項目を入力（アプリ名、サポートメール、開発者連絡先）
       * スコープを追加：`https://www.googleapis.com/auth/spreadsheets` と `https://www.googleapis.com/auth/drive`
    4. アプリケーションタイプを **Web application** に選択
    5. 認可リダイレクト URI を追加：
       * 開発環境: `http://localhost:8000/auth/google/callback`
       * 本番環境: `https://yourdomain.com/auth/google/callback`
    6. **Create** をクリック
    7. **Client ID** と **Client Secret** をコピー
  </Step>

  <Step title="Laravel 環境を設定">
    `.env` ファイルに追加：

    ```env theme={null}
    GOOGLE_CLIENT_ID=your-client-id-here
    GOOGLE_CLIENT_SECRET=your-client-secret-here
    GOOGLE_REDIRECT=http://localhost:8000/auth/google/callback
    ```

    `config/google.php` を更新：

    ```php theme={null}
    'client_id' => env('GOOGLE_CLIENT_ID', ''),
    'client_secret' => env('GOOGLE_CLIENT_SECRET', ''),
    'redirect_uri' => env('GOOGLE_REDIRECT', ''),
    'scopes' => [
        \Google\Service\Sheets::SPREADSHEETS,
        \Google\Service\Drive::DRIVE,
    ],
    'access_type' => 'offline', // リフレッシュトークンに必須
    'prompt' => 'consent select_account',
    ```
  </Step>

  <Step title="Laravel Socialite をインストール">
    ```bash theme={null}
    composer require laravel/socialite
    ```

    `config/services.php` に追加：

    ```php theme={null}
    'google' => [
        'client_id' => env('GOOGLE_CLIENT_ID'),
        'client_secret' => env('GOOGLE_CLIENT_SECRET'),
        'redirect' => env('GOOGLE_REDIRECT'),
    ],
    ```
  </Step>

  <Step title="認証コントローラーを実装">
    ```php theme={null}
    // app/Http/Controllers/AuthController.php
    <?php

    namespace App\Http\Controllers;

    use App\Models\User;
    use Illuminate\Http\Request;
    use Laravel\Socialite\Facades\Socialite;

    class AuthController extends Controller
    {
        public function redirectToGoogle()
        {
            return Socialite::driver('google')
                ->scopes(config('google.scopes'))
                ->with([
                    'access_type' => config('google.access_type'),
                    'prompt' => config('google.prompt'),
                ])
                ->redirect();
        }

        public function handleGoogleCallback()
        {
            try {
                $googleUser = Socialite::driver('google')->user();
                
                $user = User::updateOrCreate(
                    ['email' => $googleUser->email],
                    [
                        'name' => $googleUser->name,
                        'email' => $googleUser->email,
                        'google_access_token' => $googleUser->token,
                        'google_refresh_token' => $googleUser->refreshToken,
                        'google_expires_in' => $googleUser->expiresIn,
                        'google_token_created' => now()->timestamp,
                    ]
                );

                auth()->login($user);

                return redirect('/dashboard')->with('success', 'Google 認証に成功しました');
            } catch (\Exception $e) {
                return redirect('/login')
                    ->with('error', '認証失敗: ' . $e->getMessage());
            }
        }

        public function logout(Request $request)
        {
            auth()->logout();
            $request->session()->invalidate();
            $request->session()->regenerateToken();

            return redirect('/');
        }
    }
    ```
  </Step>

  <Step title="ルートを追加">
    ```php theme={null}
    // routes/web.php
    Route::get('/auth/google', [AuthController::class, 'redirectToGoogle'])
        ->name('google.redirect');
    Route::get('/auth/google/callback', [AuthController::class, 'handleGoogleCallback'])
        ->name('google.callback');
    Route::post('/logout', [AuthController::class, 'logout'])
        ->name('logout');
    ```
  </Step>

  <Step title="User モデルを更新">
    マイグレーション：

    ```php theme={null}
    // database/migrations/add_google_tokens_to_users_table.php
    Schema::table('users', function (Blueprint $table) {
        $table->text('google_access_token')->nullable();
        $table->text('google_refresh_token')->nullable();
        $table->integer('google_expires_in')->nullable();
        $table->integer('google_token_created')->nullable();
    });
    ```

    User モデル：

    ```php theme={null}
    // app/Models/User.php
    protected $fillable = [
        'name',
        'email',
        'password',
        'google_access_token',
        'google_refresh_token',
        'google_expires_in',
        'google_token_created',
    ];

    protected $hidden = [
        'password',
        'remember_token',
        'google_access_token',
        'google_refresh_token',
    ];

    public function getGoogleTokenArray(): array
    {
        return [
            'access_token' => $this->google_access_token,
            'refresh_token' => $this->google_refresh_token,
            'expires_in' => $this->google_expires_in,
            'created' => $this->google_token_created,
        ];
    }

    public function hasValidGoogleToken(): bool
    {
        return !empty($this->google_access_token) 
            && !empty($this->google_refresh_token);
    }
    ```
  </Step>

  <Step title="Sheets を使用">
    ```php theme={null}
    use Revolution\Google\Sheets\Facades\Sheets;

    public function getSheetData(Request $request)
    {
        $user = $request->user();
        
        if (!$user->hasValidGoogleToken()) {
            return redirect()->route('google.redirect');
        }

        try {
            $token = $user->getGoogleTokenArray();

            $values = Sheets::setAccessToken($token)
                ->spreadsheet('user-spreadsheet-id')
                ->sheet('Sheet1')
                ->all();
                
            return view('sheets.data', compact('values'));
        } catch (\Exception $e) {
            // トークン期限切れなら再認証に
            if (str_contains($e->getMessage(), 'invalid_grant') 
                || str_contains($e->getMessage(), 'unauthorized')) {
                return redirect()->route('google.redirect');
            }

            throw $e;
        }
    }
    ```
  </Step>
</Steps>

## トークンリフレッシュ

パッケージはトークン有効期限切れを自動的に処理します：

```php theme={null}
$token = [
    'access_token' => $user->google_access_token,
    'refresh_token' => $user->google_refresh_token,
    'expires_in' => $user->google_expires_in,
    'created' => $user->google_token_created,
];

// 期限切れなら自動的にリフレッシュ
Sheets::setAccessToken($token)
    ->spreadsheet('id')
    ->sheet('Sheet1')
    ->all();

// リフレッシュ後の更新トークンを取得
$updatedToken = Sheets::getAccessToken();
if ($updatedToken) {
    $user->update([
        'google_access_token' => $updatedToken['access_token'],
        'google_token_created' => time(),
    ]);
}
```

## ミドルウェア

Google 認証を必須にするミドルウェア：

```php theme={null}
// app/Http/Middleware/RequireGoogleAuth.php
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class RequireGoogleAuth
{
    public function handle(Request $request, Closure $next)
    {
        $user = $request->user();

        if (!$user || !$user->hasValidGoogleToken()) {
            if ($request->expectsJson()) {
                return response()->json(
                    ['error' => 'Google 認証が必須です'], 
                    401
                );
            }

            return redirect()->route('google.redirect');
        }

        return $next($request);
    }
}
```

## セキュリティ

### 1. トークン保管

* トークンはデータベースに安全に保存
* Laravel の組み込み暗号化を使用
* クライアント側にトークンを公開しない

### 2. スコープ管理

* 必要最小限のスコープのみ要求
* 最小権限の原則を適用
* ユーザーに必要な権限を明確に説明

### 3. エラーハンドリング

* 期限切れトークンを適切に処理
* ユーザーフレンドリーな再認証フローを提供
* エラーをログ記録・監視

## トラブルシューティング

### よくあるエラー

**「redirect\_uri\_mismatch」エラー**

* Google Console のリダイレクト URI とアプリケーションが完全に一致しているか確認
* http vs https の違いを確認
* 末尾のスラッシュを確認

**「invalid\_grant」または「unauthorized」エラー**

* トークンが期限切れでリフレッシュに失敗
* ユーザーに再認証を求める
* リフレッシュトークンが存在するか確認

**「access\_denied」エラー**

* ユーザーが権限許可を拒否
* 適切なメッセージで対応
* 再認証を試みるオプションを提供

### テストルート

```php theme={null}
Route::get('/test-oauth', function (Request $request) {
    $user = $request->user();
    
    if (!$user->hasValidGoogleToken()) {
        return 'Google トークンがありません。'
            . '<a href="' . route('google.redirect') . '">'
            . '認証</a>';
    }
    
    try {
        $token = $user->getGoogleTokenArray();
        $sheets = Sheets::setAccessToken($token)->spreadsheetList();
        
        return 'OAuth が機能しています！スプレッドシート数: ' 
            . count($sheets);
    } catch (\Exception $e) {
        return 'OAuth エラー: ' . $e->getMessage();
    }
})->middleware('auth');
```
