Q&A

Curl コマンドによるPushメッセージの送信ができない

前提・実現したいこと

Curl コマンドによるPushメッセージの送信を実現したい。

発生している問題・エラーメッセージ

Note: Unnecessary use of -X or --request, POST is already inferred.

  • Trying 203.104.153.64...
  • TCP_NODELAY set
  • Connected to api.line.me (203.104.153.64) port 443 (#0)
  • ALPN, offering h2
  • ALPN, offering http/1.1
  • successfully set certificate verify locations:
  • CAfile: /etc/ssl/cert.pem CApath: none
  • TLSv1.2 (OUT), TLS handshake, Client hello (1):
  • TLSv1.2 (IN), TLS handshake, Server hello (2):
  • TLSv1.2 (IN), TLS handshake, Certificate (11):
  • TLSv1.2 (IN), TLS handshake, Server key exchange (12):
  • TLSv1.2 (IN), TLS handshake, Server finished (14):
  • TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
  • TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
  • TLSv1.2 (OUT), TLS handshake, Finished (20):
  • TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
  • TLSv1.2 (IN), TLS handshake, Finished (20):
  • SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
  • ALPN, server accepted to use h2
  • Server certificate:
  • subject: C=JP; ST=Tokyo-to; L=Shinjuku-ku; O=LINE Corporation; CN=*.line.me
  • start date: Jun 17 06:01:58 2020 GMT
  • expire date: Sep 5 12:00:00 2022 GMT
  • subjectAltName: host "api.line.me" matched cert's "*.line.me"
  • issuer: C=BE; O=GlobalSign nv-sa; CN=GlobalSign RSA OV SSL CA 2018
  • SSL certificate verify ok.
  • Using HTTP2, server supports multi-use
  • Connection state changed (HTTP/2 confirmed)
  • Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
  • Using Stream ID: 1 (easy handle 0x7fd272009200)

POST /v2/bot/message/push HTTP/2 Host: api.line.me User-Agent: curl/7.64.1 Accept: / Content-Type: application/json Authorization: Bearer ${TOKEN} Content-Length: 238

  • We are completely uploaded and fine
  • Connection state changed (MAX_CONCURRENT_STREAMS == 128)! < HTTP/2 401 < server: openresty < date: Thu, 17 Dec 2020 07:06:22 GMT < content-type: application/json < x-line-request-id: bc379111-4ceb-46ac-849e-477db8b5f7ec < x-content-type-options: nosniff < x-xss-protection: 1; mode=block < cache-control: no-cache, no-store, max-age=0, must-revalidate < pragma: no-cache < expires: 0 < x-frame-options: DENY < www-authenticate: Bearer error="invalid_token", error_description="invalid token" <
  • Connection #0 to host api.line.me left intact {"message":"Authentication failed due to the following reason: invalid token. Confirm that the access token in the authorization header is valid."}* Closing connection 0

該当のソースコード

curl -v -X POST https://api.line.me/v2/bot/message/push
-H 'Content-Type: application/json'
-H 'Authorization: Bearer ${TOKEN}'
-d '{ "to": ${USERID}, "messages":[ { "type":"text", "text":"Hello, world1" }, { "type":"text", "text":"Hello, world2" } ] }'

TOKENとUSERIDは設定しています。

試したこと

Ruby ではPushメッセージを送信することができます。

  • 0
  • 2
  • 424
  • twitter facebook

該当のソースコードではシングルクオートが使用されているので、環境変数${TOKEN}が展開されずにエラーになっていると思われます。以下のようにダブルクオートに直して試してみてください。

curl -v -X POST https://api.line.me/v2/bot/message/push
-H "Content-Type: application/json"
-H "Authorization: Bearer ${TOKEN}"
-d "{ "to": "${USERID}", "messages": [ { "type": "text", "text": "Hello, world1" }, { "type": "text", "text": "Hello, world2" } ] }"

  • 0

K さん ありがとうございます。ダブルクオートに修正したところ改善しました。

** 変更後のコード ** curl -v -X POST https://api.line.me/v2/bot/message/push
-H "Content-Type: application/json"
-H "Authorization: Bearer ${TOKEN}"
-d "{ "to": ${USERID}, "messages":[ { "type":"text", "text":"Hello, world1" } ] }"

  • 0
本当によろしいですか? question.vm