ios Facebook Graph API GET 请求 - 应包含“字段”参数(Swift,Facebook SDK v4.5.1)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32031677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Facebook Graph API GET request - Should contain "fields" parameter (Swift, Facebook SDK v4.5.1)
提问by Armin
My iOS app uses Facebook's Graph API Request during sign up to get user information, which worked fine prior to upgrading to Facebook's newest SDK. After upgrading, I'm getting a runtime error stating: "FBSDKLog: starting with Graph API v2.4, GET requests for /me should contain an explicit "fields" parameter".
我的 iOS 应用程序在注册期间使用 Facebook 的 Graph API 请求来获取用户信息,在升级到 Facebook 的最新 SDK 之前运行良好。升级后,我收到一个运行时错误,指出:“FBSDKLog:从 Graph API v2.4 开始,/me 的 GET 请求应包含显式的“字段”参数”。
Here is the code:
这是代码:
func requestFacebook() {
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in
if ((error) != nil)
{
// Process error
println("Error: \(error)")
}
else if error == nil
{
let birthday : NSString = (result.valueForKey("birthday") as? NSString)!
var currentDate = NSDate()
var birthdayFormatter = NSDateFormatter()
let userCalendar = NSCalendar.currentCalendar()
birthdayFormatter.dateFormat = "MM/DD/YYYY"
var birthdayNSDate = birthdayFormatter.dateFromString(birthday as String)
var userAge = self.calculateAge(birthdayNSDate!)
PFUser.currentUser()!["age"] = userAge
var facebookID: NSString = (result.valueForKey("id") as? NSString)!
var pictureURL = "https://graph.facebook.com/\(facebookID)/picture?type=large&return_ssl_resources=1"
var URLRequest = NSURL(string: pictureURL)
var URLRequestNeeded = NSURLRequest(URL: URLRequest!)
NSURLConnection.sendAsynchronousRequest(URLRequestNeeded, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!, error: NSError!) -> Void in
if error == nil {
var picture = PFFile(data: data)
PFUser.currentUser()!["picture"] = picture
PFUser.currentUser()!.saveInBackgroundWithBlock({ (success, error) -> Void in
if error == nil {
var userPicture:PFFile = PFUser.currentUser()!.valueForKey("picture") as! PFFile
userPicture.getDataInBackgroundWithBlock { (imageData, error) -> Void in
if error == nil {
self.meProfileImageView.image = UIImage(data: imageData!)
var userName:String = PFUser.currentUser()!.valueForKey("username") as! String
var userAge:Int = PFUser.currentUser()!.valueForKey("age") as! Int
self.meLabel.text = "\(userName), \(userAge)"
self.findFriends()
}
}
} else {
println(error)
}
})
}
else {
println("Error: \(error.localizedDescription)")
}
})
}
})
}
The error message is at the line:
错误消息位于以下行:
let birthday : NSString = (result.valueForKey("birthday") as? NSString)!
This code ran perfectly fine before upgrading to Facebook's SDK v4.5.1, but I don't know what to change to make this work now. Huge thanks to anyone that can help me!
这段代码在升级到 Facebook 的 SDK v4.5.1 之前运行得非常好,但我不知道现在要更改什么才能使其正常工作。非常感谢任何可以帮助我的人!
回答by Loxx
this line needs a parameters value that is NOT nil
这一行需要一个非零的参数值
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
I'll expand more, for example, and this is psuedo code of sorts:
例如,我将扩展更多,这是各种伪代码:
so for ObjC it would be this, for example, so the the above would read the following:
例如,对于 ObjC 来说就是这样,所以上面的内容如下:
parameters:@{@"fields": @"id, name"}
In swift it would be something similar
很快,它会是类似的东西
something like this:
像这样:
parameters:["fields": "email"]
This is for just two fields, and again it's psuedo code, but you need to specify the parameters
这只是两个字段,也是伪代码,但需要指定参数
UPDATE:
更新:
Im adding this update to show folks where you find the parameters for the fields as described above, here's a quick link:
我添加此更新以向人们展示您可以在何处找到上述字段的参数,这是一个快速链接:
Here's the Paramters field info (this should stay roughly static with FBSDK, some of this is "core" which means it won't really change that much, but I'll update whenever I can):
这是参数字段信息(这应该与 FBSDK 保持大致静态,其中一些是“核心”,这意味着它不会真正改变那么多,但我会尽可能更新):
https://developers.facebook.com/docs/graph-api/reference/user
https://developers.facebook.com/docs/graph-api/reference/user
The format of the info below is the following, sort of, I just did this quickly:
以下信息的格式如下,有点,我只是很快做到了:
"parameter""type"
“参数” “类型”
"description"
“描述”
idnumeric string
The id of this person's user account. This ID is unique to each app and cannot be used across different apps. Our upgrade guide provides more information about app-specific IDs
aboutstring
The About Me section of this person's profile
age_rangeAgeRange
The age segment for this person expressed as a minimum and maximum age. For example, more than 18, less than 21.
biostring
The person's bio
birthdaystring
The person's birthday. This is a fixed format string, like MM/DD/YYYY. However, people can control who can see the year they were born separately from the month and day so this string can be only the year (YYYY) or the month + day (MM/DD)
contextUserContext
Social context for this person
currencyCurrency
The person's local currency information
deviceslist
The list of devices the person is using. This will return only iOS and Android devices
educationlist
The person's education
emailstring
The person's primary email address listed on their profile. This field will not be returned if no valid email address is available
favorite_athleteslist
Athletes the person likes
favorite_teamslist
Sports teams the person likes
first_namestring
The person's first name
genderstring
The gender selected by this person, male or female. This value will be omitted if the gender is set to a custom value
hometownPage
The person's hometown
inspirational_peoplelist
The person's inspirational people
install_typeenum
Install type
installedbool
Is the app making the request installed?
interested_inlist
Genders the person is interested in
is_shared_loginbool
Is this a shared login (e.g. a gray user)
is_verifiedbool
People with large numbers of followers can have the authenticity of their identity manually verified by Facebook. This field indicates whether the person's profile is verified in this way. This is distinct from the verified field
languageslist
Facebook Pages representing the languages this person knows
last_namestring
The person's last name
linkstring
A link to the person's Timeline
locationPage
The person's current location as entered by them on their profile. This field is not related to check-ins
localestring
The person's locale
meeting_forlist
What the person is interested in meeting for
**middle_name string
The person's middle name
namestring
The person's full name
CoreDefault
name_formatstring
The person's name formatted to correctly handle Chinese, Japanese, or Korean ordering
payment_pricepointsPaymentPricepoints
The person's payment pricepoints
test_groupunsigned int32
Platform test group
politicalstring
The person's political views
relationship_statusstring
The person's relationship status
religionstring
The person's religion
security_settingsSecuritySettings
Security settings
significant_otherUser
The person's significant other
sportslist
Sports this person likes
quotesstring
The person's favorite quotes
third_party_idstring
A string containing an anonymous, but unique identifier for the person. You can use this identifier with third parties
timezonefloat (min: -24) (max: 24)
The person's current timezone offset from UTC
token_for_businessstring
A token that is the same across a business's apps. Access to this token requires that the person be logged into your app. This token will change if the business owning the app changes
updated_timedatetime
Updated time
shared_login_upgrade_required_bydatetime
The time that the shared loginneeds to be upgraded to Business Manager by
verifiedbool
Indicates whether the account has been verified. This is distinct from the is_verified field. Someone is considered verified if they take any of the following actions:
Register for mobile Confirm their account via SMS Enter a valid credit card
video_upload_limitsVideoUploadLimits
Video upload limits
viewer_can_send_giftbool
Can the viewer send a gift to this person?
websitestring
The person's website
worklist
Details of a person`s work experience
public_keystring
The person's PGP public key
coverCoverPhoto
id数字字符串
此人的用户帐户的 ID。此 ID 对每个应用程序都是唯一的,不能在不同的应用程序之间使用。我们的升级指南提供了有关应用特定 ID 的更多信息
关于字符串
此人个人资料的“关于我”部分
age_range年龄范围
此人的年龄段表示为最小和最大年龄。比如大于18,小于21。
生物串
个人简介
生日串
人的生日。这是一个固定格式的字符串,如 MM/DD/YYYY。然而,人们可以控制谁可以看到他们出生的年份与月和日分开,所以这个字符串只能是年 (YYYY) 或月 + 日 (MM/DD)
上下文用户 上下文
此人的社会背景
货币货币
此人的当地货币信息
设备清单
此人正在使用的设备列表。这将仅返回 iOS 和 Android 设备
教育清单
人的教育
电子邮件字符串
此人的主要电子邮件地址列在其个人资料中。如果没有可用的有效电子邮件地址,则不会返回此字段
favorite_athletes名单
喜欢的运动员
favorite_teams列表
喜欢的运动队
名字字符串
此人的名字
性别字符串
此人选择的性别,男性或女性。如果性别设置为自定义值,则此值将被省略
家乡页面
人的家乡
励志人物名单
人的励志人
install_type枚举
安装类型
安装布尔值
是否安装了发出请求的应用程序?
感兴趣的名单
该人感兴趣的性别
is_shared_login布尔值
这是共享登录吗(例如灰色用户)
is_verifiedbool
拥有大量粉丝的人可以通过 Facebook 手动验证其身份的真实性。此字段指示此人的个人资料是否以这种方式验证。这与验证字段不同
语言列表
Facebook 页面代表此人所知道的语言
姓氏字符串
此人的姓氏
链接字符串
指向此人时间线的链接
位置页面
此人在其个人资料中输入的当前位置。此字段与签到无关
语言环境字符串
人的地方
会议列表
这个人有兴趣见面的原因
**middle_name 字符串
此人的中间名
名字串
此人的全名
核心默认值
name_format字符串
人名格式正确处理中文、日文或韩文的排序
payment_pricepointsPaymentPricepoints
该人的付款价格点
test_group无符号 int32
平台测试组
串
个人的观点
关系状态字符串
该人的关系状态
宗教字符串
人的宗教
security_settings安全设置
安全设定
重要_其他用户
该人的另一半
运动清单
这个人喜欢的运动
引号字符串
这个人最喜欢的名言
third_party_id字符串
包含此人的匿名但唯一标识符的字符串。您可以将此标识符用于第三方
时区浮动(最小:-24)(最大:24)
该人的当前时区与 UTC 的偏移量
token_for_business字符串
在企业的应用程序中相同的令牌。访问此令牌需要此人登录您的应用程序。如果拥有该应用程序的企业发生变化,此令牌将发生变化
更新时间日期时间
更新时间
shared_login_upgrade_required_by日期时间
共享登录需要升级到商务管理器的时间
验证布尔值
指示帐户是否已通过验证。这与 is_verified 字段不同。如果某人采取以下任何行动,则被视为经过验证:
Register for mobile Confirm their account via SMS Enter a valid credit card
video_upload_limitsVideoUploadLimits
视频上传限制
viewer_can_send_giftbool
观众可以给这个人送礼物吗?
网站字符串
该人的网站
工作清单
一个人的工作经历的细节
公钥字符串
此人的 PGP 公钥
封面封面照片
Also, here's the current "Edge" stuff: The format for "Edge' stuff is this, roughly, I did this quickly:
此外,这是当前的“边缘”内容:“边缘”内容的格式大致是这样的,我很快就做到了:
"Edge"
“边缘”
"Description"
“描述”
favorite_requests
Developers' favorite requests to the Graph API
request_history
Developers' Graph API request history
accounts
Facebook Pages this person administers/is an admin for
achievements
Achievements made in Facebook games
adaccounts
The advertising accounts to which this person has access
adaccountgroups
Ad account groups
adcontracts
The person's ad contracts
admined_groups
Groups the user admins
adnetworkanalytics
Insights data for the person's Audience Network apps
albums
The photo albums this person has created
apprequests
This person's pending requests from an app
apprequestformerrecipients
App requests
books
The books listed on this person's profile
domains
The domains the user admins
events
Events for this person. By default this does not include events the person has declined or not replied to
family
This person's family relationships.
stream_filters
A list of filters that can be applied to the News Feed edge
friendlists
The person's custom friend lists
ids_for_business
Business entities can claim ownership of multiple apps using the Business Manager. This edge returns the list of IDs that this user has in any of those other apps
invitable_friends
A list of friends that can be invited to install a Facebook Canvas app
games
Games this person likes
groups
The Facebook Groups that the person belongs to
likes
All the Pages this person has liked
movies
Movies this person likes
music
Music this person likes
objects
Objects
permissions
The permissions that the person has granted this app
photos
Photos the person is tagged in or has uploaded
picture
The person's profile picture
tagged_places
List of tagged places for this person. It can include tags on videos, posts, statuses or links
promotable_domains
All the domains user can promote
promotable_events
All the events which user can promote.
taggable_friends
Friends that can be tagged in content published via the Graph API
television
TV shows this person likes
videos
Videos the person is tagged in or uploaded
video_broadcasts
Video broadcasts from this person
applications
The Facebook apps that this person is a developer of.
checkins
The checkins this person has made.
feed
The feed of posts (including status updates) and links published by this person.
friendrequests
A person's pending friend requests.
friends
A person's friends.
home
A person's Facebook homepage feed.
inbox
A person's Facebook Messages inbox.
locations
A feed of posts and photos that include location information and in which this person has been tagged. This is useful for constructing a chronology of places that the person has visited.
mutualfriends
The list of mutual friends between two people.
notifications
The unread Facebook notifications that a person has.
outbox
A person's Facebook Messages outbox.
questions
The questions that a person has created.
scores
The scores this person has received from Facebook Games that they've played.
subscribers
The profiles that are following this person.
subscribedto
The profile that this person is following.
收藏_请求
开发人员最喜欢的 Graph API 请求
request_history
开发人员的图形 API 请求历史记录
帐户
此人管理/是其管理员的 Facebook 主页
成就
在 Facebook 游戏中取得的成就
帐户
此人有权访问的广告帐户
帐户组
广告帐户组
合同
此人的广告合同
admined_groups
将用户管理员分组
广告网络分析
此人的 Audience Network 应用的洞察数据
专辑
此人创建的相册
应用请求
此人来自应用的待处理请求
应用请求接收者
应用请求
图书
此人的个人资料中列出的书籍
域
用户管理的域
事件
此人的事件。默认情况下,这不包括此人拒绝或未回复的事件
家庭
此人的家庭关系。
流过滤器
可应用于 News Feed 边缘的过滤器列表
好友列表
此人的自定义好友列表
ids_for_business
业务实体可以使用商务管理器声明对多个应用程序的所有权。此边缘返回此用户在任何其他应用程序中的 ID 列表
invitable_friends
可以邀请安装 Facebook Canvas 应用程序的朋友列表
游戏
这个人喜欢的游戏
团体
此人所属的 Facebook 群组
喜欢
此人喜欢的所有主页
电影
这个人喜欢的电影
音乐
这个人喜欢的音乐
对象
对象
权限
此人已授予此应用程序的权限
相片
该人被标记或已上传的照片
图片
此人的个人资料图片
tagged_places
此人的标记地点列表。它可以包含视频、帖子、状态或链接上的标签
promotable_domains
用户可以推广的所有域
promotable_events
用户可以推广的所有事件。
taggable_friends
可以在通过 Graph API 发布的内容中标记的朋友
电视
此人喜欢的电视节目
视频
该人被标记或上传的视频
video_broadcasts
来自此人的视频广播
应用
此人是其开发者的 Facebook 应用程序。
签到
此人所做的签到。
喂养
此人发布的帖子(包括状态更新)和链接的提要。
好友请求
一个人的待处理好友请求。
朋友们
一个人的朋友。
家
一个人的 Facebook 主页提要。
收件箱
一个人的 Facebook 消息收件箱。
地点
包含位置信息且此人已被标记的帖子和照片的提要。这对于构建一个人去过的地方的年表很有用。
共同的朋友
两个人的共同好友列表。
通知
一个人拥有的未读 Facebook 通知。
发件箱
一个人的 Facebook 消息发件箱。
问题
一个人提出的问题。
分数
此人从他们玩过的 Facebook 游戏中获得的分数。
订户
关注此人的个人资料。
订阅
此人正在关注的个人资料。
回答by Hugo Perez
use "fields" as Key and then comma separate your values
使用“字段”作为键,然后用逗号分隔您的值
let params = ["fields": "email, friends"]
回答by José Miguel Galván
In Obj-C insert in the logInWithReadPermissions method:
在 Obj-C 中插入 logInWithReadPermissions 方法:
parameters:@{@"fields": @"id, name, email"}
if you leave nil in parameters you only receive id and name in the result object, although you have set for example:
如果您在参数中保留 nil,您只会在结果对象中收到 id 和 name,尽管您已经设置了例如:
NSArray *permissionsArray = @[ @"email", @"public_profile"];
It′s not very clear in the documentation, hope it can helps.
文档里写的不是很清楚,希望能帮到你。
回答by Paula Vasconcelos Gueiros
I had the same problem, but adding field options would generate a request error when fetching the user photo. I solved this problem by adding a simple parameters:@{@"fields": @""}
to the code:
我有同样的问题,但添加字段选项会在获取用户照片时产生请求错误。我通过parameters:@{@"fields": @""}
在代码中添加一个简单的来解决这个问题:
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:[NSString stringWithFormat:@"me/picture"]
parameters:@{@"fields": @""}
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection,
id result,
NSError *error) {
if (!error) {
// success
} else {
// fail
}}
];
回答by dylankbuckley
In Swift, you can declare multiple parameters like this:
在 Swift 中,您可以像这样声明多个参数:
let graphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "email, first_name, last_name, gender, picture"])