本文介绍了如何使用Swifter授权Twitter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释如何使用Swifter for iOS成功授权Twitter(oAuth)。我正在尝试创建一个时间表。

Could someone please explain to me how to successfully authorize Twitter (oAuth) with Swifter for iOS. I am trying to create a timeline.

let twitterAccount = Swifter(consumerKey: "CONSUMER-KEY", consumerSecret: "CONSUMER-SECRET")

然后我需要授权,而且我对如何这样做很困惑。

Then I need to authorize, and I'm quite confused on how to do so.

swifter.authorizeWithCallbackURL(callbackURL, success: {
(accessToken: SwifterCredential.OAuthAccessToken?, response: NSURLResponse) in

// ...

},
failure: {
    (error: NSError) in

    // ...

})

我收到一条错误,上面写着未解决的callbackURL标识符,这显然是因为我没有定义它,但是当我这样做时说'呼叫中的额外参数'。还有更简单的授权方式吗?在此之后,我应该如何在get-home-timeline部分中获取用户screen-name / user-id。我知道我做的事情完全错了,我对这一切都很陌生,所以任何帮助都会受到赞赏。

I get an error saying 'Unresolved Identifier of callbackURL' which is obviously because I haven't defined it, but when I do it says 'extra argument in call'. Also is there any easier way of Authorizing? After this how am I supposed to get the users screen-name/user-id to use in the get-home-timeline part. I know I'm doing something completely wrong and I am quite new to all of this, so any help would be appreciated.

推荐答案

也许你已经找到了答案,但我正在为那些仍在寻找答案的人写信。

Maybe you have found an answer, but I am writing here for those who are still looking for it.

swifter.authorizeWithCallbackURL(NSURL(string: "swifter://success")!, success: {
            (accessToken: SwifterCredential.OAuthAccessToken?, response: NSURLResponse) in

            println("Access Token key \(accessToken?.key)")
            println("userId\(accessToken?.userID)")
            println("userName\(accessToken?.screenName)")
            println("Access Token secret\(accessToken?.secret)")
            //Save the values that you need in NSUserDefaults 
            },
            failure: failureHandler)

你可以使用你喜欢的任何 callbackURL 。但是,要让您的应用响应此网址,您必须进行一些更改。在 AppDelegate 类中添加以下内容。不要忘记导入 SwifteriOS

You can use any callbackURL that you like. But for your app to respond to this URL you must do some changes. In the AppDelegate class add the following. Don't forget to import SwifteriOS

func application(application: UIApplication!, openURL url: NSURL!, sourceApplication: String!, annotation: AnyObject!) -> Bool {
        Swifter.handleOpenURL(url)

        return true
    }

现在在Project Explorer中点击你的项目,选择 info 标签。在底部,您将看到 URL类型。展开它,然后单击加号。在 URL Schemes 中输入您的 callbackURL 方案。在此示例中,其 swifter 并在标识符中编写您的应用程序标识符。

Now click on your project in the Project Explorer, select the info tab. At the bottom you will see URL Types. Expand it and click the plus sign. In the URL Schemes enter your callbackURL scheme. In this example its swifter and in identifier write your app identifier.

现在在任何其他 ViewController 中,您需要先调用以下内容,然后才能调用任何需要身份验证的API。

Now in any other ViewController you need to call following before you can call any API that need Authentication.

required init(coder aDecoder: NSCoder) {

        var oauthToken : String = NSUserDefaults.standardUserDefaults().valueForKey("oauth_token") as String
        var oauthTokenSecret : String  = NSUserDefaults.standardUserDefaults().valueForKey("oauth_secret") as String

        self.swifter = Swifter(consumerKey: Twitter["consumerKey"]!, consumerSecret: Twitter["consumerSecret"]!, oauthToken: oauthToken, oauthTokenSecret: oauthTokenSecret)
        super.init(coder: aDecoder)
    }

在此之后,您可以发送消息,发布推文,创建友谊等。

After this you can send message, post a tweet, create friendship etc.

这篇关于如何使用Swifter授权Twitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 12:55