使用facebook oauth2.0在Grails 2.3.8应用程序中例外

时间:2014-05-12 16:34:31

标签: grails oauth-2.0 facebook-oauth grails-2.3

我正在设置以下内容:

BuildConfig:

plugins {
  build ":tomcat:7.0.52.1"
  compile ":hibernate:3.6.10.13"
  compile ':spring-security-core:2.0-RC2'
  compile ':spring-security-oauth:2.0.2'
  compile ':spring-security-oauth-facebook:0.1'
}

配置:

grails.plugin.springsecurity.oauth.domainClass = 'com.mobilizr.OAuthID'

oauth{
  providers{
    facebook {
      api = org.scribe.builder.api.FacebookApi
      key = '111111'
      secret = '333veeerysecret23234234234'
      successUri = '/oauth/facebook/success'
      failureUri = '/oauth/facebook/error'
      callback = "${baseURL}/oauth/facebook/callback"
    }
  }
}

点击链接时:

<oauth:connect provider="facebook" id="facebook-connect-link">Facebook</oauth:connect>

我得到例外:

java.lang.IllegalArgumentException: Must provide a valid url as callback. Facebook does not support OOB

我不知道这应该是什么意思。页面spring security oauth facebook并未给出太多详细信息。有没有人有想法,或者可能是FB-auth的工作样本?

1 个答案:

答案 0 :(得分:2)

回调URL中的

baseURL是您的应用程序服务器URL。将baseURL替换为您的应用程序服务器URL。例如,

callback = "http://localhost:8080/mySampleApp/oauth/facebook/callback"

并解决了您的错误。

还可以替换successUrifailureUri。例如,

oauth {
  providers {
    facebook {
        api = org.scribe.builder.api.FacebookApi
        key = 'Your_Key'
        secret = 'Your_Secret'
        successUri = 'http://localhost:8080/mySampleApp/oauthCallback/success'
        failureUri = 'http://localhost:8080/mySampleApp/oauthCallback/error'
        callback = "http://localhost:8080/mySampleApp/oauth/facebook/callback"
    }
  }
}

并提供从Facebook获取数据的代码。我编写了代码来获取用户信息:

import grails.converters.JSON
import org.scribe.model.Token

class OauthCallbackController {

def oauthService

def index() {}

def success() {
    Token facebookAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('facebook')]
    if (facebookAccessToken) {
        def facebookResource = oauthService.getFacebookResource(facebookAccessToken, "https://graph.facebook.com/me")
        def facebookResponse = JSON.parse(facebookResource?.getBody())

        Map data = [id: facebookResponse.id, username: facebookResponse.username, name: facebookResponse.name, email: facebookResponse.email,
                first_name: facebookResponse.first_name, last_name: facebookResponse.last_name, birthday: facebookResponse.birthday,
                gender: facebookResponse.gender, link: facebookResponse.link, work: facebookResponse.work, hometown: facebookResponse.hometown,
                education: facebookResponse.education]

        render data
    } else {
        flash.error = "Token not found."
        render view: '/index'
    }
}

def error() {
    render params
}
}

Working Code Repository


希望这会有所帮助: - )