Facebook 测试用户无法发帖

发布于 2024-12-02 07:25:45 字数 5459 浏览 0 评论 0原文

对于我们的网站,我们做了很多自动化测试。因此,最近我刚刚使用 Facebook Graph API 编写了一个方法,该方法可以在 feed 上创建墙贴。 当我使用真实的 Facebook 帐户进行现场测试时,此方法有效。但是,当我使用 facebook 测试用户 (权限设置为“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 和访问令牌),但这仍然困扰着我。我真的很好奇你认为问题可能是什么。

For our website, we do a lot of automated tests. So, recently I've just written a method using Facebook Graph API that create a wall post on feed. This method works when I test it live using real facebook accounts. However, when I use facebook test users (with permission set to "publish_stream"), then I get 403 forbidden.

Are "test users" not allowed to make wall post? or is there something that I am not doing right?

This is my test code written in 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)
}

This test makes two test users/accounts and make them friends of each other, then testUser1 post something in testUser2's wall. It fails in line: assertNotNull postResponse.

This is the header of the response:

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"
   }
}

The user is created as such:

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
}

and the post message goes like this:

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
}

Even though it works using real facebook accounts (by filling in the id and access token manually), this still bothers me. I'm genuinely curious about you think the problem might be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄖ落Θ余辉 2024-12-09 07:25:45

在 createTestAccount() 中,查询参数“permission”应为“permissions”(复数形式)。

In your createTestAccount(), the query parameter 'permission' should be 'permissions' (in plural form).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文