|
6 | 6 | Twift is an asynchronous Swift library for the Twitter v2 API. |
7 | 7 |
|
8 | 8 | - [x] No external dependencies |
9 | | -- [x] Only one callback-based method ([`Authentication.requestUserCredentials`](https://github.com/daneden/Twift/wiki/Twift_Authentication#requestusercredentialsclientcredentialscallbackurlpresentationcontextproviderwith)) |
| 9 | +- [x] Fully async |
10 | 10 | - [x] Full Swift type definitions/wrappers around Twitter's API objects |
11 | 11 |
|
12 | 12 | ## Quick Start Guide |
13 | 13 |
|
14 | | -New `Twift` instances must be initiated with either User Access Tokens or an App-Only Bearer Token: |
| 14 | +New `Twift` instances must be initiated with either OAuth 2.0 user authentication or an App-Only Bearer Token: |
15 | 15 |
|
16 | 16 | ```swift |
17 | 17 | // User access tokens |
18 | | -let clientCredentials = OAuthCredential(key: CONSUMER_KEY, secret: CONSUMER_SECRET) |
19 | | -let userCredentials = OAuthCredential(key: ACCESS_KEY, secret: ACCESS_SECRET) |
20 | | -let userAuthenticatedClient = Twift(.userAccessTokens(clientCredentials: clientCredentials, userCredentials: userCredentials) |
| 18 | +let oauthUser: OAuth2User = OAUTH2_USER |
| 19 | +let userAuthenticatedClient = Twift(.oauth2UserAuth(oauthUser: oauthUser) |
21 | 20 |
|
22 | 21 | // Bearer token |
23 | 22 | let appOnlyClient = Twift(.appOnly(bearerToken: BEARER_TOKEN) |
24 | 23 | ``` |
25 | 24 |
|
26 | | -You can acquire user access tokens by authenticating the user with `Twift.Authentication().requestUserCredentials()`: |
| 25 | +You can authenticating users with `Twift.Authentication().authenticateUser()`: |
27 | 26 |
|
28 | 27 | ```swift |
29 | 28 | var client: Twift? |
30 | 29 |
|
31 | | -Twift.Authentication().requestUserCredentials( |
32 | | - clientCredentials: clientCredentials, |
33 | | - callbackURL: URL(string: "twift-test://")! |
34 | | -) { (userCredentials, error) in |
35 | | - if let creds = userCredentials { |
36 | | - client = Twift(.userAccessTokens(clientCredentials: clientCredentials, userCredentials: creds)) |
37 | | - } |
| 30 | +let (oauthUser, error) = await Twift.Authentication().authenticateUser( |
| 31 | + clientId: TWITTER_CLIENT_ID, |
| 32 | + redirectUri: URL(string: TWITTER_CALLBACK_URL)!, |
| 33 | + scope: Set(OAuth2Scope.allCases) |
| 34 | +) |
| 35 | + |
| 36 | +if let oauthUser = oauthUser { |
| 37 | + client = Twift(.oauth2UserAuth(oauthUser)) |
38 | 38 | } |
39 | 39 | ``` |
40 | 40 |
|
@@ -125,29 +125,3 @@ let me = response?.data |
125 | 125 | // The user's pinned Tweet |
126 | 126 | let tweet = response?.includes?.tweets.first |
127 | 127 | ``` |
128 | | - |
129 | | -### Optional Actor IDs |
130 | | - |
131 | | -Many of Twift's methods require a `User.ID` in order to make requests on behalf of that user. For convenience, this parameter is often marked as optional, since the currently-authenticated `User.ID` may be found on the instance's authentication type: |
132 | | - |
133 | | -```swift |
134 | | -var client: Twift? |
135 | | -var credentials: OAuthCredential? |
136 | | - |
137 | | -Twift.Authentication().requestUserCredentials( |
138 | | - clientCredentials: clientCredentials, |
139 | | - callbackURL: URL(string: "twift-test://")! |
140 | | -) { (userCredentials, error) in |
141 | | - if let userCredentials = userCredentials { |
142 | | - client = Twift(.userAccessTokens(clientCredentials: clientCredentials, userCredentials: userCredentials)) |
143 | | - credentials = userCredentials |
144 | | - } |
145 | | -} |
146 | | - |
147 | | -// Elsewhere in the app... |
148 | | - |
149 | | -// These two calls are equivalent since the client was initialized with an OAuthCredential containing the authenticated user's ID |
150 | | -let result1 = try? await client?.followUser(sourceUserId: credentials.userId!, targetUserId: "12") |
151 | | -let result2 = try? await client?.followUser(targetUserId: "12") |
152 | | - |
153 | | -``` |
0 commit comments