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?

More than 1 year has passed since last update.

VSCodeのターミナル(PowerShell)からcurlコマンドがエラーになる時の対策

Posted at

概要

  • Fitbit APIからアクセストークンを取得する際にターミナルからcurlコマンドを入力する必要がある。
  • 公式サイトのcurlコマンドのコピペだと、PowerShellが"--"を単項演算子扱いしてエラーになる。
  • その時の対策方法を簡単にまとめた。

環境

Windows10
Visual Studio Code(1.71.2)
VSCodeのターミナル:PowerShell

Fitbit APIのサイトのどこの部分か?

ここです。
image.png

修正前のcurlコマンド

curl -i -X POST \
 -H 'Authorization: Basic hogehogehogehogehogehogehogehoge' \
 --data "clientId=999HogeHoge" \
 --data "grant_type=authorization_code" \
 --data "redirect_uri=http%1234567890hogehoge" \
 --data "code=hugahugahugahugahugahuga" \
 -H 'Content-Type: application/x-www-form-urlencoded' \
https://api.fitbit.com/oauth2/token

これをそのままコピペしてターミナル(PowerShell)で実行するとエラーになる。
原因はPowerShellが数式Modeになっているから。

実行結果(エラー)

発生場所 行:3 文字:4
+  --data "clientId=999HogeHoge" \
+    ~
単項演算子 '--' の後に式が存在しません。
発生場所 行:3 文字:4
+  --data "clientId=999HogeHoge" \
+    ~~~~
式またはステートメントのトークン 'data' を使用できません。
発生場所 行:3 文字:8
+  --data "clientId=999HogeHoge" \
+        ~
Data セクションにステートメント ブロックがありません。
発生場所 行:3 文字:32
+  --data "clientId=999HogeHoge" \
+                                ~
式またはステートメントのトークン '\' を使用できません。
発生場所 行:4 文字:4
+  --data "grant_type=authorization_code" \
+    ~
単項演算子 '--' の後に式が存在しません。
発生場所 行:4 文字:4
+  --data "grant_type=authorization_code" \
+    ~~~~
式またはステートメントのトークン 'data' を使用できません。
発生場所 行:4 文字:8
+  --data "grant_type=authorization_code" \
+        ~
Data セクションにステートメント ブロックがありません。
発生場所 行:4 文字:41
+  --data "grant_type=authorization_code" \
+                                         ~
式またはステートメントのトークン '\' を使用できません。
発生場所 行:5 文字:4
+  --data "redirect_uri=http%1234567890hogehoge" \
+    ~
単項演算子 '--' の後に式が存在しません。
発生場所 行:5 文字:4
+  --data "redirect_uri=http%1234567890hogehoge" \
+    ~~~~
式またはステートメントのトークン 'data' を使用できません。
報告されていない解析エラーもあります。報告されたエラーを修正して再試行してください。
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingExpressionAfterOperator

解決方法

以下のコマンドに書き換えればOK。
演算子ではなく、文字列扱いになるようにします。

修正後のcurlコマンド

$CurlArgument = '-i',
'-X', 'POST',
'-H', 'Authorization: Basic hogehogehogehogehogehogehogehoge',
'--data', 'clientId=999HogeHoge',
'--data', 'grant_type=authorization_code',
'--data', 'redirect_uri=http%1234567890hogehoge',
'--data', 'code=hugahugahugahugahugahuga',
'-H', 'Content-Type: application/x-www-form-urlencoded',
'https://api.fitbit.com/oauth2/token'
$CURLEXE = 'curl.exe'
& $CURLEXE @CurlArgument

実行結果(成功!)

HTTP/1.1 200 OK
Date: Wed, 28 Sep 2022 09:08:06 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Origin,Accept-Encoding
Cache-control: no-cache, private
Set-Cookie: fct=hugahugahugahugahugahuga; Path=/; Secure; HttpOnly
Set-Cookie: JSESSIONID=hoge99hoge99hoge99hoge99.fitbit1; Path=/; Secure; HttpOnly
Content-Language: ja-JP
X-Frame-Options: SAMEORIGIN
Via: 1.1 google
CF-Cache-Status: DYNAMIC
Server: cloudflare
CF-RAY: 9999hogehoge-NRT

{"access_token":"hogehogehogehogehogehogehogehoge","expires_in":28800,"refresh_token":"hugahugahugahugahugahuga","scope":"temperature settings location oxygen_saturation activity profile respiratory_rate heartrate social weight sleep nutrition","token_type":"Bearer","user_id":"9999hogetaro"}

参考リンク

【StackOverFlow】How to use the curl command in PowerShell?

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?