gpt4 book ai didi

ios - 如何在 swift 中使用 GraphAPI 从 facebook 页面获取帖子

转载 作者:可可西里 更新时间:2023-11-01 00:52:55 25 4
gpt4 key购买 nike

我的应用程序有一个功能:从 Facebook 页面获取帖子并将其显示在应用程序中。我搜索了一下,发现我应该使用 Facebook GraphAPI: https://developers.facebook.com/docs/graph-api/using-graph-api/v2.3#reading我点击前面的链接,但出现错误:“请求此资源需要访问 token 。”我有页面的访问 token ,但我真的不知道如何在我的请求中分配它。有什么帮助吗?非常感谢!

最佳答案

<强>1。使用 Facebook SDK。

登录 FACEBOOK 按钮

@IBAction func click_Facebook(sender: AnyObject)
{
let loginView : FBSDKLoginManager = FBSDKLoginManager()

loginView.loginBehavior = FBSDKLoginBehavior.Browser

loginView.logInWithReadPermissions(["public_profile","user_friends","user_photos","user_location","user_education_history","user_birthday","user_posts"], handler: { (result : FBSDKLoginManagerLoginResult!, error : NSError!) -> Void in

if ((error) != nil)
{
// Process error

self.alertWithMessaage(error.localizedDescription)
}
else if result.isCancelled {
// Handle cancellations
}
else {

self.returnUserData()
}

})

}

登录后调用以下函数。

    var Requset : FBSDKGraphRequest

println("\(FBSDKAccessToken.currentAccessToken())")

var acessToken = String(format:"%@", FBSDKAccessToken.currentAccessToken().tokenString) as String

println("\(acessToken)")

var parameters1 = ["access_token":FBSDKAccessToken.currentAccessToken().tokenString]


Requset = FBSDKGraphRequest(graphPath:"me/posts", parameters:parameters1, HTTPMethod:"GET")

Requset.startWithCompletionHandler({ (connection, result, error) -> Void in

MBProgressHUD.hideHUDForView(appDelegate.window, animated: true)

if ((error) != nil)
{
println("Error: \(error)")
}
else
{
println("fetched user: \(result)")

var dataDict: AnyObject = result!.objectForKey("data")!

}
})
}

<强>2。使用默认的 Facebook 应用程序。

//获取访问 token

    var _accountStore: ACAccountStore = ACAccountStore()

var accountType : ACAccountType = _accountStore.accountTypeWithAccountTypeIdentifier(ACAccountTypeIdentifierFacebook)!

var accounts : NSArray = _accountStore.accountsWithAccountType(accountType)!

var facebookAccount1: ACAccount = accounts.lastObject! as ACAccount

println("== \(facebookAccount1.credential.oauthToken)")

//将AccessToken传递给参数

    var acessToken = String(format:"%@", facebookAccount1.credential.oauthToken) as String

var parameters = ["access_token":acessToken] as NSDictionary

//SLRequset

    var imageURL : NSURL = NSURL(string: "Your graph URL")!

println("=== TOKEN : \(LoginService().currentAccount?.credential.oauthToken)")

var Requset = SLRequest(forServiceType: SLServiceTypeFacebook, requestMethod: SLRequestMethod.GET, URL: imageURL, parameters: parameters)

Requset.account = account

Requset.performRequestWithHandler { (responseData : NSData!, urlResponse : NSHTTPURLResponse!, error : NSError!) -> Void in


if (error != nil)
{
self.alertWithMessaage(error.localizedDescription)
}
else{

var datastring = NSString(data: responseData, encoding: NSUTF8StringEncoding)

println("== \(datastring)")

var error:NSError? = nil

var UserDict = NSJSONSerialization.JSONObjectWithData(responseData, options:nil, error: &error) as? NSDictionary


}

}

关于ios - 如何在 swift 中使用 GraphAPI 从 facebook 页面获取帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31022760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com