IOS TWTRComposer request error: unauthorized (401)
With the following code and a few other variations I've tried, I always get an unauthorized error. I am using TwitterKit 3.0 with CocoaPods. I have my plist setup, my Twitter app set up, and code like this:
// In didFinishLaunchingWithOptions
Twitter.sharedInstance().start(withConsumerKey:"XX", consumerSecret:"YYY")
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
{
return Twitter.sharedInstance().application(app, open: url, options: options)
}
// In response to a click
Twitter.sharedInstance().logIn { session, error in
if session != nil { // Log in succeeded
let composer = TWTRComposer()
composer.setText(tweetText)
composer.show(from: self.navigationController!) { result in
if (result == .done) {
print("Successfully composed Tweet")
} else {
print("Cancelled composing")
}
}
}
}
"Has there been an error sending Tweet message: Error Domain = TWTRNetworkingErrorDomain Code = -1011" Request error: Unauthorized (401) "UserInfo = {NSLocalizedFailureReason =, TWTRNetworkingStatusCode = 401, NSErrorFailingURLKey = https://api.twitter.com/1 /update.json , NSLocalizedDescription = Request failed: unauthorized (401) "
source to share
When used, TWTRComposer
there is no reason to login. The SDK will use the current session in the Twitter app, if there is one, or it will intelligently brush it off. I can't tell you why your login is failing, but I would suggest just deleting it to get around the problem.
let composer = TWTRComposer()
composer.setText(tweetText)
composer.show(from: self.navigationController!) { (result in
if (result == .done) {
print("Successfully composed Tweet")
} else {
print("Cancelled composing")
}
}
You said you already configured everything, but I'll give you an installation checklist in case you skipped a step:
-
Initialize Twitter Bundle
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { Twitter.sharedInstance().start(withConsumerKey:"xxx", consumerSecret:"xxx") return true }
-
Configure Info.Plist
// Info.plist <key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>twitterkit-<consumerKey></string> </array> </dict> </array> <key>LSApplicationQueriesSchemes</key> <array> <string>twitter</string> <string>twitterauth</string> </array>
If you still have problems, make sure you check the status of your app at https://apps.twitter.com/ . There are several ways to restrict or even disable your app key. If so, there will be a red label below the app name.
source to share
I am also facing the same problem. I used Twitter, FB and Google login. the problem is the plist sequence of its URLSchemes
I added twitter circuits first when it worked
my ex is
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>twitterkit-xxxx</string>
</array>
</dict>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fbxxxxx</string>
</array>
</dict>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>com.googleusercontent.apps.xxxx-xxxx</string>
</array>
</dict>
</array>
source to share
In TwitterKit 3.3.0 basically :
Although the callback url will not be requested by the Twitter Kit in your app, it must be set for a valid app url to work with the SDK.
although when trying to create a Twitter app it doesn't say it is required - which it is if you want the stream below to work
Failure to do so will break down the entire stream and nothing will happen automatically:
If Twitter is not installed on the device, it automatically falls back to using OAuth through the web view (using SFSafariViewController for the first user and UIWebView for subsequent additional users.)
What will work if you don't have callback_url:
Only if the user has the Twitter app installed can we compose a tweet or log in - the rest just doesn't work.
When the Twitter app is not installed :
- When used,
composer.show(from: self) { (result) in
you will receive.cancelled
. - When used,
TWTRTwitter.sharedInstance().logIn(completion: { (session, error) in
you will receiveRequest failed: unauthorized (401)
General Notes:
-
If the API key / secret is invalid, any action taken by TwitterKit will crash the app
-
Make sure the singleton method
start("key", "secret")
and the projectplist
with the value:twitterkit-{API KEY}
have the same key, otherwise your application will crash.
source to share