「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.02.26
XML
カテゴリ: Graph API


# アクセストークン取得のための認証情報

$client_id = "YOUR_CLIENT_ID"

$client_secret = "YOUR_CLIENT_SECRET"

$tenant_id = "YOUR_TENANT_ID"

$redirect_uri = "http://localhost"


# アクセストークンを取得するためのパラメータを設定

$authParams = @{

    client_id     = $client_id

    scope         = "https://graph.microsoft.com/Files.ReadWrite.All"

    response_type = "code"

    redirect_uri  = $redirect_uri

}


# ユーザーにログインしてアクセストークンを取得

$authUrl = "https://login.microsoftonline.com/$tenant_id/oauth2/v2.0/authorize?" + ($authParams | foreach { "$($_.Name)=$($_.Value)" })

Start-Process -FilePath $authUrl


# ユーザーによる認証と承認が完了し、リダイレクトされたら、リダイレクトURIからコードを取得します。


# コードを入力

$code = Read-Host -Prompt "Enter the authorization code"


# アクセストークンを取得

$tokenParams = @{

    client_id     = $client_id

    scope         = "https://graph.microsoft.com/Files.ReadWrite.All"

    code          = $code

    redirect_uri  = $redirect_uri

    grant_type    = "authorization_code"

    client_secret = $client_secret

}


$tokenResponse = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$tenant_id/oauth2/v2.0/token" -Body $tokenParams


# アクセストークンを取得

$accessToken = $tokenResponse.access_token


if (-not [string]::IsNullOrEmpty($accessToken)) {

    Write-Host "Access Token: $accessToken"


    # 共有するファイルの ID

    $fileId = "YOUR_FILE_ID"


    # 共有先のユーザーのメールアドレス

    $shareEmailAddress = "recipient@example.com"


    # 共有の設定

    $shareBody = @{

        recipients = @(

            @{

                emailAddress = @{

                    address = $shareEmailAddress

                }

            }

        )

        message = "This is the shared file."

        requireSignIn = $false

        sendInvitation = $true

    } | ConvertTo-Json


    $shareFileUrl = "https://graph.microsoft.com/v1.0/me/drive/items/$fileId/createLink"

    $shareFileResponse = Invoke-RestMethod -Uri $shareFileUrl -Method Post -Headers @{

        Authorization = "Bearer $accessToken"

        'Content-Type' = 'application/json'

    } -Body $shareBody


    if ($shareFileResponse) {

        Write-Host "File shared successfully. Shared link: $($shareFileResponse.link.webUrl)"

    } else {

        Write-Host "Failed to share file."

    }

} else {

    Write-Host "Failed to retrieve access token."

}







お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.02.26 03:43:46


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

© Rakuten Group, Inc.
Create a Mobile Website
スマートフォン版を閲覧 | PC版を閲覧
Share by: