3

我正在尝试使用 grails oauth 插件与 Google 建立联系。以下是我的代码

配置文件

oauth {
  providers {
    google {
        api = org.scribe.builder.api.GoogleApi
        key = 'XXX.apps.googleusercontent.com'
        secret = 'XXXXXXX'

        scope = 'https://www.googleapis.com/auth/userinfo.profile'
        callback = "${grails.serverURL}/oauth/google/callback"
        successUri = "${grails.serverURL}/oauthCallBack/google"
    }
  }
}

grails.google.api.url = "https://accounts.google.com/o/oauth2/auth"

OauthCallBackController.groovy

class OauthCallBackController {

  def oauthService

  def google() {
    Token googleAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('google')]
    def googleResource = oauthService.getGoogleResource(googleAccessToken, grailsApplication.config.grails.google.api.url)
    def googleResponse = JSON.parse(googleResource?.getBody())

    log.info "googleAccessToken = ${googleAccessToken}"
    log.info "googleResponse = ${googleResponse}"
    log.info "accesstoken = ${googleAccessToken.token}"

    def googleResourceDetailed = oauthService.getGoogleResource(googleAccessToken, "https://www.googleapis.com/oauth2/v1/userinfo?access_token=${googleAccessToken.token}")
    def googleResponseDetailed = JSON.parse(googleResourceDetailed?.getBody())

    log.info "googleResourceDetailed = ${googleResourceDetailed}"
    log.info "googleResponseDetailed = ${googleResponseDetailed}"

    render params
  }
}

当我尝试连接时,谷歌要求我允许应用程序访问我的凭据。在允许我的回调操作执行后(oauthCallBack/google),但我没有得到我帐户的数据。

以下是我在日志中的输出

INFO  example.OauthCallBackController  - googleAccessToken = Token[1/xxx , yyy]
INFO  example.OauthCallBackController  - googleResponse = [:]
INFO  example.OauthCallBackController  - accesstoken = 1/xxx
INFO  example.OauthCallBackController  - googleResourceDetailed = org.scribe.model.Response@2a088ca
INFO  example.OauthCallBackController  - googleResponseDetailed = [error:[message:Invalid Credentials, errors:[[message:Invalid Credentials, location:Authorization, reason:authError, locationType:header, domain:global]], code:401]]

我不知道我错在哪里以及为什么我会收到错误作为响应。

如何从谷歌获取数据?

4

1 回答 1

4

最后..,。

经过大量的谷歌搜索,我终于找出了我的问题并成功地从谷歌获得了数据。

问题出grails.google.api.url在我的配置文件中的条目中。

正确的值为

grails.google.api.url = "https://www.googleapis.com/oauth2/v1/userinfo"

现在我的代码是

配置文件

oauth {
  providers {
    google {
      api = org.scribe.builder.api.GoogleApi
      key = 'XXX.apps.googleusercontent.com'
      secret = 'XXXXXXX'

      scope = 'https://www.googleapis.com/auth/userinfo.profile'
      callback = "${grails.serverURL}/oauth/google/callback"
      successUri = "${grails.serverURL}/oauthCallBack/google"
    }
  }
}

grails.google.api.url = "https://www.googleapis.com/oauth2/v1/userinfo"

OauthCallBackController.groovy

def google() {
    Token googleAccessToken = (Token) session[oauthService.findSessionKeyForAccessToken('google')]
    def googleResource = oauthService.getGoogleResource(googleAccessToken, grailsApplication.config.grails.google.api.url)
    def googleResponse = JSON.parse(googleResource?.getBody())

    log.info "googleAccessToken = ${googleAccessToken}"
    log.info "googleResponse = ${googleResponse}"
    log.info "accesstoken = ${googleAccessToken.token}"
    log.info "id = ${googleResponse.id}"
    log.info "name = ${googleResponse.name}"

    render params
}

我的日志输出是

INFO  example.OauthCallBackController  - googleAccessToken = Token[1/xxx , yyy]
INFO  example.OauthCallBackController  - googleResponse = [id:xxxxx, locale:en, link:yyyyy, name:MKB, gender:male, family_name:B, given_name:M]
INFO  example.OauthCallBackController  - accesstoken = 1/xxx
INFO  example.OauthCallBackController  - id = xxxxx
INFO  example.OauthCallBackController  - name = MKB

Grails Oauth Plugin Demo

于 2013-09-17T17:04:50.507 回答