iOS Twitter Kit usage gets error 'Request failed: forbidden'
Asked Answered
B

4

10

I have quite similar issue to this one and I cannot find an answer as that question wasn't solved.

The problem is that I get this in Xcode's console after setting up Twitter Kit and Fabric, adding secret and api keys for twitter to plist:

Error Domain=TwitterAPIErrorDomain Code=200 "Request failed: forbidden (403)" UserInfo=0x7fdd9ac48ef0 {NSErrorFailingURLKey=https://api.twitter.com/1.1/guest/activate.json, NSLocalizedDescription=Request failed: forbidden (403), NSLocalizedFailureReason=Twitter API error : Forbidden. (code 200)}

My code that uses Twitter Kit and produces the error mentioned above:

NSString *userID = [Twitter sharedInstance].sessionStore.session.userID;
TWTRAPIClient *client = [[TWTRAPIClient alloc] initWithUserID:userID];

[client loadUserWithID:kTwitterUser completion:^ (TWTRUser *user, NSError *error) {
     if (error) {
        NSLog(@"ERROR CARL %@", error);
     } else {
        NSLog(@"User %@", user);
     }
}];
Billposter answered 18/9, 2015 at 13:33 Comment(1)
I has same issue but solution given this link worked for me https://mcmap.net/q/1163395/-error-code-403-when-using-guest-authentication-with-twitterkit-ios-for-getting-user-timelineLacker
G
4

This is a pretty old question but I wanted to put an answer out there in case anyone comes across this.

This error is more or less indicating that your "consumerKey" and "consumerSecret" are incorrect.

The goofy thing is that there's 3 ways of generating this and they all produce a different key. One is through Twitter dev website, one is created automatically when you install TwitterKit via Fabric app and one is through the Fabric website, when you activate TwitterKit.

All of the variations of the TwitterKit consumerKey/consumerSecrets did not work for me except for the one that I created on the Fabric website.

Log in to the Fabric website, find your app and tap on it and on the left panel select Add Kit. Get through adding the kit and then go back to the app home page and tap on the kit you just added, it should have the correct keys/secrets that you should be using.

Also, make sure you're using this same set of keys in the Plist TwitterKit entry and the AppDelegate initialization.

Gottuard answered 30/5, 2016 at 23:41 Comment(3)
Mike from Fabric here. Just to clarify a few things - the one key created automatically when installing through Twitter Kit is your Fabric API key. The one created through the website or when installing the Twitter Kit on a new Fabric account are the Twitter consumer key and secret which determine your permissions for accessing data on Twitter. Hope that helps a bit!Euchologion
Hi William , I'm still having this issue , the keys is correct and I added it at appdelegate and plist file , what can I do , ThanksKarlsruhe
Hey @NadaGamal are you sure you're using the key from the Fabric website and not those other keys? Also, a recent update to TwitterKit broke my installation and I haven't got it working again yet, so that may be related.Gottuard
V
7

We experienced the same 403 forbidden error code with our Android and iOS apps. The cause is a recent API change, which requires whitelisting of the following URL schemes for the apps on https://developer.twitter.com/en/apps/YOUR-APP-ID :

TwitterKit for Android: twittersdk://

TwitterKit for iOS: twitterkit-YOUR_CONSUMER_KEY://

Source: https://github.com/twitter/twitter-kit-android/issues/134

Although the answer does most likely not solve the above mentioned problem in that particular case, it is still relevant to the problem described.

Vingtetun answered 12/2, 2019 at 18:52 Comment(0)
T
5

Solution for '[TwitterKit] Error obtaining user auth token. error: Request failed: forbidden (403)'

Below is the step by step guide for Twitter login with TwitterKit in Swift 4 and above without Fabric.

Step 1: You must have a valid Twitter a/c. Login to the a/c. ==> Goto https://developer.twitter.com/en/apps/ ==> Create new app ==> Give all the details. Remember all the details are necessary hence fill it carefully. The Call back URL is extremely important and having a specific format like 'twitterkit-CONSUMER API KEY://' (You will get once created the app). ==> Create the app. ==> Click on you app details that you just created. You will see 'Keys & Tokens'. Select this to see you app Consumer Key & Secret. This will necessary into your iOS project.

Step 2: Open your Xcode project and create a single view application. Give any name to your project and click Next. By default there a ViewController.swift class. Goto Storyboard of that ViewController and create a 'Login to Twitter' button or you can create a button by code. In the button action made by you write the below code.

     // Swift
        TWTRTwitter.sharedInstance().logIn(completion: { (session, error) in
            if (session != nil) {
                print("signed in as \(session!.userName)");
            } else {
                print("error: \(error!.localizedDescription)");
            }
        })
    }

Step 3: Install TwitterKit and Firebase by pod. pod 'TwitterKit' pod 'Firebase/Core'

Step 4: After creating your app and following all the steps, don’t forget to add below provided XML code in your app’s info.plist file in the Xcode. For this, just click on your info.plist file and select Open as a Source Code. Now, just after the tag, insert the following XML snippet into it:

<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>

Now, replace the ConsumerKey with your own consumer key(Twitter API key) which you get in your Twitter application dashboard for your newly created app.

Step 5: Open your AppDelegate.swift file and add following line of code in its first application function i.e.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Twitter.sharedInstance().start(withConsumerKey:“#################”, consumerSecret:“##############################”)
return true
}

Also add one more function here: func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { return Twitter.sharedInstance().application(app, open: url, options: options) }

Step 6: Goto https://firebase.google.com ==> Click GO TO CONSOLE ==> Add Project ==> Give project name ==> Accept Terms & Cond. ==> Create Project ==> Project will be created ==> Select Continue ==> Select iOS ==> Give Bundle id, Nick name and Click on Register app button ==> Download the configuration file & Move the GoogleService-Info.plist file you just downloaded into the root of your Xcode project and add it to all targets. ==> Next ==> No need to install Firebase as you already installed this via pod. Click Next ==> import Firebase in your AppDelegate file at the top & paste 'FirebaseApp.configure()' in below function in AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?){
FirebaseApp.configure()
}

Step 7: Uninstall the app you just made if you ran it in your device previously and reinstall. ==> Wait for 2 mins . ==> You will get a message 'Congratulations, you have successfully added Firebase into your app'. This message is necessary which means a success. Continue to Console

Step 8: Select you Authentication ==> Select Sign-in method ==> Select Twitter & make it enabled with API key & Secret. Click Save.

Step 9: Click on 'Login to Twitter' button and you will see the app is redirecting to the Authorization page.

Great.. All Done.. Thank you..

Tegan answered 18/9, 2015 at 13:33 Comment(0)
G
4

This is a pretty old question but I wanted to put an answer out there in case anyone comes across this.

This error is more or less indicating that your "consumerKey" and "consumerSecret" are incorrect.

The goofy thing is that there's 3 ways of generating this and they all produce a different key. One is through Twitter dev website, one is created automatically when you install TwitterKit via Fabric app and one is through the Fabric website, when you activate TwitterKit.

All of the variations of the TwitterKit consumerKey/consumerSecrets did not work for me except for the one that I created on the Fabric website.

Log in to the Fabric website, find your app and tap on it and on the left panel select Add Kit. Get through adding the kit and then go back to the app home page and tap on the kit you just added, it should have the correct keys/secrets that you should be using.

Also, make sure you're using this same set of keys in the Plist TwitterKit entry and the AppDelegate initialization.

Gottuard answered 30/5, 2016 at 23:41 Comment(3)
Mike from Fabric here. Just to clarify a few things - the one key created automatically when installing through Twitter Kit is your Fabric API key. The one created through the website or when installing the Twitter Kit on a new Fabric account are the Twitter consumer key and secret which determine your permissions for accessing data on Twitter. Hope that helps a bit!Euchologion
Hi William , I'm still having this issue , the keys is correct and I added it at appdelegate and plist file , what can I do , ThanksKarlsruhe
Hey @NadaGamal are you sure you're using the key from the Fabric website and not those other keys? Also, a recent update to TwitterKit broke my installation and I haven't got it working again yet, so that may be related.Gottuard
P
0

I'm facing the same issue in my case the twitter developer account setup it app setup as essential in case of essential it is limited access that why this error comes up. Simply just change the account type from Essential to Elevated and you are good to go. Login to your twitter developer account and select app then scroll down and you can see to update account type to Elevated select that and fill the asked info and you submit and the check. And one more point to recheck you "Api key" and "Api secrete key" should be the same as showing your your twitter developer portal.

Callback url is setup like:

Note:- twitterkit-YOUR_CONSUMER_KEY:// (Must like this)

More info: https://developer.twitter.com/en/support/twitter-api/developer-account

https://developer.twitter.com/en/support/twitter-api/v2

For detailed info you can also checkout the required informations by using below url.

https://twittercommunity.com/t/action-required-sign-in-with-twitter-users-must-whitelist-callback-urls/105342

Changes are required because after twitter api's updations.

Perfectible answered 22/6, 2022 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.