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?

googleapiのOAuth認証

Last updated at Posted at 2024-09-17

googleapiのOAuth認証

googleapiのOAuth認証を行う方法について個人的にハマった部分をまとめます.

今回はspreadsheetを操作するためにgoogleapiを使いました.

1. googleapiのOAuth認証

この記事が一番わかりやすかったです.

基本的にこの通りに進めていくと問題ないです.

以下に注意点をまとめます.

認証コードを使ってアクセストークンを取得する時

webアプリケーションを選んでしまうと,リダイレクトURIが必要になります.

リダイレクトUTIはhttp://localhost:8080などを使ってください.

デスクトップアプリケーションを選ぶと,リダイレクトURIは不要です.

ブラウザで認証コードとリフレッシュトークンを取得する時

そのままコピーすると改行が入ってしまうので以下のようにしてください.

https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/[scope_url]&redirect_uri=http://localhost:8080&client_id=[client_id]&response_type=code&approval_prompt=force&access_type=offline

scope_urlは記事にも書いてありますが,spreadsheetsとしてください.

なので,例えば以下のようになります.

https://accounts.google.com/o/oauth2/v2/auth?scope=https://www.googleapis.com/auth/spreadsheets&redirect_uri=http://localhost:8080&client_id=1111111111111-xxxxxxxxxxxx.apps.googleusercontent.com&response_type=code&approval_prompt=force&access_type=offline

curlコマンドでアクセストークンを取得する時
client_secretが誤字っているので注意してください.

curl -X POST -d 'code=[auth_code]' -d 'client_id=[client_id]' -d 'client_secret=[client_secret]' -d 'grant_type=authorization_code' -d 'redirect_uri=http://localhost:8080' https://www.googleapis.com/oauth2/v4/token

これによって,client_idclient_secretrefresh_tokenを取得することができます.

これでgoogleapiを使う準備が整いました.

2. googleapiを使う

すでにあるspreadsheetを操作するという状況を考えます.

すでにある場合,spreadsheetIdが必要になります.

これは,spreadsheetのURLを見ると/d//editの間にある文字列です.

例えばspreadsheetのURLが以下の場合

https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxx/edit?gid=0#gid=0

xxxxxxxxxxxxxxxxxxxspreadsheetIdです.

テスト環境の場合,テストユーザーとしてspreadsheetのユーザー以外のアカウントを登録していると,spreadsheetを編集するためには,シートの権限が必要です.
右上の共有ボタンから,アクセスできる人をリンクを知っている全員にしてください.

image.png

spreadsheetのセルの値を取得するだけであれば,編集者ではなく,閲覧者で十分です.

ただし,編集者でないと,spreadsheetのセルの値を編集・更新することはできません.

これで,googleapiを使ってspreadsheetを操作する準備が整いました.

3. 実際に試してみる

できたと言っても実際に試してみたいものです.

ここでは簡単にできていることを確かめるためにcurlコマンドを使ってみます.

アクセストークンはリフレッシュトークンを取得するときに取得したものを使います.

GETリクエスト

curl --http1.1 -X GET 'https://sheets.googleapis.com/v4/spreadsheets/{spreadsheet_id}/values/{sheet_name}!A1:B2' -H 'Authorization: Bearer {access_token}'

{spreadsheet_id}はspreadsheetのIDです.

{sheet_name}はシートの名前です.

{access_token}は取得したアクセストークンです.

これで,sheet_nameA1:B2のセルの値を取得することができます.

POSTリクエスト

curl --http1.1 -X PUT 'https://sheets.googleapis.com/v4/spreadsheets/{spreadsheet_id}/values/{sheet_name}!C1:C2?valueInputOption=USER_ENTERED' \
-H 'Authorization: Bearer {access_token}' \
-H 'Content-Type: application/json' \
-d '{"values": [["hoge"], ["fuga"]]}'

{spreadsheet_id}はspreadsheetのIDです.

{sheet_name}はシートの名前です.

{access_token}は取得したアクセストークンです.

これで,sheet_nameC1:C2のセルにhogefugaを入力することができます.

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?