Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Twitter Kit usage gets error 'Request failed: forbidden'

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);
     }
}];
like image 785
Yevheniia Zelenska Avatar asked Sep 18 '15 13:09

Yevheniia Zelenska


2 Answers

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.

like image 166
Tim Schäfer Avatar answered Nov 03 '22 08:11

Tim Schäfer


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

like image 5
2 revs Avatar answered Nov 03 '22 10:11

2 revs