agorapulse / grails-facebook-sdk

Facebook SDK Grails Plugin
http://agorapulse.github.com/grails-facebook-sdk/guide
30 stars 13 forks source link

userAccessToken is not set correctly #31

Closed confile closed 11 years ago

confile commented 11 years ago

I tried to upload an image to the users facebook wall. The problem is that the first time I try it I get the error below. The second time went fine. The problem seems to be in the userAccessToken which is not set correctly the first time by:

def userAccessToken = facebookContext.user.token 

What is wrong here?

The code I use is:

        try {
            // publish info to facebook wall of user
            // For private data (access token required)
            def userAccessToken = facebookContext.user.token

                        // if no token is available
            if (!userAccessToken) {
                def facebookUser = FacebookUser.findByUser(springSecurityService.currentUser)
                userAccessToken = facebookUser.accessToken
            }
            def facebookClient = new FacebookGraphClient(userAccessToken)

            // Publish an existing remote file by URL to user's Facebook wall
            def linkUrl = "http://www.testurl.com"

            def messageText = "Wall text"
            def publishPhotoResponse = facebookClient.publish("me/photos", [message: messageText], "logo.jpg", new URL(linkUrl).openStream())
        println "Published photo ID: " + publishPhotoResponse.id

        }
        catch(Exception e) {
            log.error "Failed to publish foto on facebook", e
        }

This is the error I got the first time:

Message: Received Facebook error response of type OAuthException: An active access token must be used to query information about the current user.
    Line | Method
->>  695 | exceptionForTypeAndMessage                      in com.restfb.DefaultFacebookClient$DefaultGraphFacebookExceptionMapper
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|    617 | throwFacebookResponseStatusExceptionIfNecessary in com.restfb.DefaultFacebookClient
|    559 | makeRequestAndProcessResponse . . . . . . . . . in     ''
|    522 | makeRequest                                     in     ''
|    290 | publish . . . . . . . . . . . . . . . . . . . . in     ''
|     50 | publish                                         in grails.plugin.facebooksdk.FacebookGraphClient
|    178 | save . . . . . . . . . . . . . . . . . . . . .  in com.myapp.TestController
|    565 | handle                                          in org.eclipse.jetty.servlet.ServletHolder
|   1360 | doFilter . . . . . . . . . . . . . . . . . . .  in org.eclipse.jetty.servlet.ServletHandler$CachedChain
|   1331 | doFilter                                        in     ''
|    477 | doHandle . . . . . . . . . . . . . . . . . . .  in org.eclipse.jetty.servlet.ServletHandler
|    119 | handle                                          in org.eclipse.jetty.server.handler.ScopedHandler
|    539 | handle . . . . . . . . . . . . . . . . . . . .  in org.eclipse.jetty.security.SecurityHandler
|    227 | doHandle                                        in org.eclipse.jetty.server.session.SessionHandler
|   1031 | doHandle . . . . . . . . . . . . . . . . . . .  in org.eclipse.jetty.server.handler.ContextHandler
|    406 | doScope                                         in org.eclipse.jetty.servlet.ServletHandler
|    186 | doScope . . . . . . . . . . . . . . . . . . . . in org.eclipse.jetty.server.session.SessionHandler
|    965 | doScope                                         in org.eclipse.jetty.server.handler.ContextHandler
|    117 | handle . . . . . . . . . . . . . . . . . . . .  in org.eclipse.jetty.server.handler.ScopedHandler
|    288 | forward                                         in org.eclipse.jetty.server.Dispatcher
|    115 | forward . . . . . . . . . . . . . . . . . . . . in     ''
|   1331 | doFilter                                        in org.eclipse.jetty.servlet.ServletHandler$CachedChain
|    477 | doHandle . . . . . . . . . . . . . . . . . . .  in org.eclipse.jetty.servlet.ServletHandler
|    119 | handle                                          in org.eclipse.jetty.server.handler.ScopedHandler
|    521 | handle . . . . . . . . . . . . . . . . . . . .  in org.eclipse.jetty.security.SecurityHandler
|    227 | doHandle                                        in org.eclipse.jetty.server.session.SessionHandler
|   1031 | doHandle . . . . . . . . . . . . . . . . . . .  in org.eclipse.jetty.server.handler.ContextHandler
|    406 | doScope                                         in org.eclipse.jetty.servlet.ServletHandler
|    186 | doScope . . . . . . . . . . . . . . . . . . . . in org.eclipse.jetty.server.session.SessionHandler
|    965 | doScope                                         in org.eclipse.jetty.server.handler.ContextHandler
|    117 | handle . . . . . . . . . . . . . . . . . . . .  in org.eclipse.jetty.server.handler.ScopedHandler
|    111 | handle                                          in org.eclipse.jetty.server.handler.HandlerWrapper
|    349 | handle . . . . . . . . . . . . . . . . . . . .  in org.eclipse.jetty.server.Server
|    449 | handleRequest                                   in org.eclipse.jetty.server.AbstractHttpConnection
|    925 | content . . . . . . . . . . . . . . . . . . . . in org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler
|    857 | parseNext                                       in org.eclipse.jetty.http.HttpParser
|    235 | parseAvailable . . . . . . . . . . . . . . . .  in     ''
|     76 | handle                                          in org.eclipse.jetty.server.AsyncHttpConnection
|    609 | handle . . . . . . . . . . . . . . . . . . . .  in org.eclipse.jetty.io.nio.SelectChannelEndPoint
|     45 | run                                             in org.eclipse.jetty.io.nio.SelectChannelEndPoint$1
|    599 | runJob . . . . . . . . . . . . . . . . . . . .  in org.eclipse.jetty.util.thread.QueuedThreadPool
|    534 | run                                             in org.eclipse.jetty.util.thread.QueuedThreadPool$3
^    722 | run . . . . . . . . . . . . . . . . . . . . . . in java.lang.Thread
benorama commented 11 years ago

You should debug you tokens to check their validity.

Use this to check if they are valid: https://developers.facebook.com/tools/debug

By default, token are only valid during 1 hour. You need to exchange it to get longer one (valid during 2 months) if you want to persist to DB.

confile commented 11 years ago

@benorama how do I get longer tokens with your plugin? And how can I refresh the token?

benorama commented 11 years ago

Use facebookContest.user.exchangeToken(), it will replace the current token (short term) by a long term (2 month) token.

confile commented 11 years ago

@benorama The problem is that after I loggin with facebook and use facebookClient.publish the error occurs. The token cannot be to old. When I user facebookClient.publish the second time as I wrote above it worked fine.

Do have any idea? Thank you for your help.