本文介绍了如何使用 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

    // ...

})

我收到一条错误消息,说回调URL 的未解析标识符",这显然是因为我还没有定义它,但是当我这样做时,它说调用中的额外参数".还有没有更简单的授权方式?在此之后,我应该如何让用户屏幕名称/用户 ID 用于 get-home-timeline 部分.我知道我在做一些完全错误的事情,而且我对这一切都很陌生,所以任何帮助将不胜感激.

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.但是为了让您的应用响应此 URL,您必须进行一些更改.在 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 并在 identifier 中写入您的应用程序标识符.

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