对于我们的网站,我们进行了大量的自动化测试。所以,最近我刚刚使用Facebook Graph API编写了一个方法,在feed上创建了一个墙贴。 当我使用真实的Facebook帐户实时测试时,此方法有效。但是,当我使用facebook test users(权限设置为“publish_stream”)时,我会被禁止403。
“测试用户”是否不允许发布墙贴?还是有什么东西我做得不对?
这是我用groovy编写的测试代码
void testPostMessageOnWall() {
def appAccessToken = facebookService.getAppAccessToken()
assertNotNull appAccessToken
def testUser1 = facebookService.createTestAccount("Ryoko UserOne", appAccessToken)
assertNotNull testUser1
def testUser2 = facebookService.createTestAccount("Ryoko UserTwo", appAccessToken)
assertNotNull testUser2
def response = facebookService.connectTwoTestAccount(testUser1.id, testUser1.access_token, testUser2.id, testUser2.access_token)
assertTrue response
println testUser1
println testUser2
def postResponse = facebookService.postMessageOnWall([accessToken:testUser1.access_token,
from:testUser1.id,
to:testUser2.id,
message:"Join ryoko.it. It's nice there!",
link:"http://ryoko.it",
name:"name of website",
caption:"ryoko.it",
description:"description",
picture:"http://ryoko.it/images/ryoko.png"
])
println postResponse
assertNotNull postResponse
facebookService.deleteTestAccount(testUser1.id, testUser1.access_token)
facebookService.deleteTestAccount(testUser2.id, testUser2.access_token)
}
这个测试使两个测试用户/帐户成为彼此的朋友,然后testUser1在testUser2的墙上发布了一些东西。它失败了:assertNotNull postResponse
。
这是回复的标题:
Date: Thu, 01 Sep 2011 18:39:10 GMT
WWW-Authenticate: OAuth "Facebook Platform" "insufficient_scope" "(#200) The user hasn't authorized the application to perform this action"
P3P: CP="Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p"
X-Cnection: close
X-FB-Rev: 433230
Content-Length: 146
Pragma: no-cache
X-FB-Server: 10.64.212.43
Content-Type: text/javascript; charset=UTF-8
Cache-Control: no-store
Expires: Sat, 01 Jan 2000 00:00:00 GMT
data:
{
"error": {
"type": "OAuthException",
"message": "(#200) The user hasn't authorized the application to perform this action"
}
}
用户是这样创建的:
def createTestAccount(fullname, appAccessToken) {
def accessToken = appAccessToken
def urlString = "${GRAPH_API_URL}/${APP_ID}/accounts/test-users?installed=true"
def encodedFullname = URLEncoder.encode(fullname, "UTF-8")
urlString += "&name=${encodedFullname}"
urlString += "&permission=create_note,email,offline_access,photo_upload,publish_stream,read_friendlists,share_item,status_update,video_upload"
urlString += "&method=post"
urlString += "&access_token=${accessToken}"
def url = new URL(urlString)
def connection = url.openConnection()
def userDetails
if (connection.responseCode == 200) {
userDetails = JSON.parse(connection.content.text)
}
else {
println "[FACEBOOK]\tResponse code ${connection.responseCode}: ${connection.responseMessage} [${urlString}]"
}
userDetails
}
并且帖子信息如下:
def postMessageOnWall(params) {
assert params.accessToken
assert params.from
assert params.to
def content = "access_token=${postEncode(params.accessToken)}"
if (params.message) content += "&message=${postEncode(params.message)}"
if (params.link) content += "&link=${postEncode(params.link)}"
if (params.name) content += "&name=${postEncode(params.name)}"
if (params.caption) content += "&caption=${postEncode(params.caption)}"
if (params.description) content += "&description=${postEncode(params.description)}"
if (params.picture) content += "&picture=${postEncode(params.picture)}"
if (params.from) content += "&from=${postEncode(params.from)}"
if (params.to) content += "&to=${postEncode(params.to)}"
def urlString = "${GRAPH_API_URL}/${params.to}/feed"
def url = new URL(urlString)
def connection = url.openConnection()
connection.doOutput = true
connection.setRequestMethod("POST")
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")
connection.setRequestProperty("Content-Length", "${content.size()}")
println content
def writer = new OutputStreamWriter(connection.outputStream)
writer.write(content)
writer.flush()
writer.close()
connection.connect()
def response
if (connection.responseCode == 200) {
response = JSON.parse(connection.content.text)
}
else {
println "[FACEBOOK]\tResponse code ${connection.responseCode}: ${connection.responseMessage} [${urlString}]"
}
println "response: ${response}"
response
}
即使它使用真实的Facebook帐户(通过手动填写id和访问令牌),这仍然困扰我。我真的很好奇你认为问题可能是。
答案 0 :(得分:0)
在您的createTestAccount()中,查询参数' permission'应该是'权限' (复数形式)。