mironal / TwitterAPIKit

Swift library for the Twitter API v1 and v2 🍷
MIT License
118 stars 23 forks source link

OAuth2 #113

Closed mironal closed 2 years ago

mironal commented 2 years ago
mironal commented 2 years ago

For Confidential Clients

The following is an explanation of how to authenticate with OAuth 2.0 when Confidential Clients are selected on the Twitter Developer Portal.

以下のように Twitter Developer Portal で Confidential Clients を選択した場合の OAuth 2.0 での認証方法を説明します.

image

Walkthrough (手順)

1. GET oauth2/authorize

https://twitter.com/i/oauth2/authorize に適切なパラメータを付けてリクエストし、Twitter の認証画面(下記スクリーンショット)を開きます。

Parameters

https://developer.twitter.com/en/docs/authentication/oauth-2-0/authorization-codeParameters の部分を参照

redirect_uri は Developer Portal で設定したものと同じにしないとエラーになります.

Response

認証すると redirect_uristatecode がついた状態でリダイレクトされます.

ex) https://www.example.com?state=state&code=VGNibzFWSWREZm01bjN1N3dicWlNUG1oa2xRRVNNdmVHelJGY2hPWGxNd2dxOjE2MjIxNjA4MjU4MjU6MToxOmFjOjE

スクリーンショット

image

2. POST oauth2/token - Access Token

https://api.twitter.com/2/oauth2/token に適切なパラメータを付けてリクエストします。

リクエスト例は以下の通り.

curl --location --request POST 'https://api.twitter.com/2/oauth2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'code=VGNibzFWSWREZm01bjN1N3dicWlNUG1oa2xRRVNNdmVHelJGY2hPWGxNd2dxOjE2MjIxNjA4MjU4MjU6MToxOmFjOjE' \
--data-urlencode 'grant_type=authorization_code' \
--data-urlencode 'client_id=rG9n6402A3dbUJKzXTNX4oWHJ' \
--data-urlencode 'redirect_uri=https://www.example.com' \
--data-urlencode 'code_verifier=challenge'

https://developer.twitter.com/en/docs/authentication/oauth-2-0/user-access-token

mironal commented 2 years ago

For Public Clients

image