1

我一直在尝试使用 SimpleAuth 创建一个 iOS 应用程序来验证 Instagram。

在我的 AppDelegate.swift 我有:

SimpleAuth.configuration()["instagram"] = ["client_id": "MY-CLIENT-ID", SimpleAuthRedirectURIKey: "MY-REDIRECT-URL"]

(显然在需要的地方插入客户端 ID 和重定向 URI)

在我的 ViewController.swift 我有:

    @IBAction func instagramAuthenticate(sender: AnyObject) {

    SimpleAuth.authorize("instagram", options: ["scope" : ["likes"]], completion: {
        (responseObject : AnyObject!, error : NSError!) -> Void in

        println("\(responseObject)")
    })
}

由于某种原因,当用户授权我的应用程序时,responseObject 似乎返回“nil”。可能的意思是出了点问题。

我对 Swift/iOS 比较陌生,不确定我做错了什么。谢谢

4

1 回答 1

1

原来我的重定向 URI 是它返回 nil 的原因。我改变了它,一切都很完美!感谢以上评论中的 HorseT。

编辑:下面更深入的答案

可以在此处找到以下所有信息。

首先,您必须在您的AppDelegate.swift

//Instagram Confifuration
    SimpleAuth.configuration()["instagram"] = ["client_id": "YOUR-CLIENT-ID", SimpleAuthRedirectURIKey: "myApp://Auth/instagram"]

确保SimpleAuthRedirectURIKey等于instagram 开发者页面上您的应用设置中的设置。

在此处输入图像描述

接下来,您需要授权用户。

SimpleAuth.authorize("instagram", options: ["scope" : ["basic", "comments", "likes", "relationships"]], completion: {
(responseObject : AnyObject!, error : NSError!) -> Void in
if (responseObject != nil) {
    var instagramResponse = responseObject as! NSDictionary
    var accessToken : String = instagramResponse["credentials"]!["token"] as! String
    println(accessToken)
} else {
    println(error.localizedDescription)
}
}

这只是获取响应并打印 accessToken。

从这里开始,您可以访问端点以检索更多数据。

希望这会有所帮助!

于 2015-01-01T22:57:54.143 回答