我有一个Rails网站,有谷歌OAUTH2实施和工作。
我们正在开发一个iOS应用程序,它将使用API与我的web服务器进行对话。有些API需要对用户进行身份验证。这个想法是,iOS应用程序在设备上使用OAUTH2对用户进行身份验证,然后通过SSL将令牌从设备发布到web作为身份验证。我需要网站来验证令牌。
在Google API控制台中,我添加了iPhone设备的客户端ID,并通过转到以下位置获得了访问令牌:
https://accounts.google.com/o/oauth2/auth?
scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&
redirect_uri=urn:ietf:wg:oauth:2.0:oob&
response_type=code&
client_id={my client id}然后,我将令牌传递到我的站点。在我的网站上,我通过以下方式验证令牌:
google = OmniAuth::Strategies::GoogleOauth2.new(ENV['GOOGLE_API_KEY'],['GOOGLE_CLIENT_API_SECRET'])
client = OAuth2::Client.new(ENV['GOOGLE_API_KEY'],['GOOGLE_CLIENT_API_SECRET'], google.options.client_options)
access_token = OAuth2::AccessToken.new(client, params[:token], google.options.access_token_options || {})
google.access_token = access_token
google.auth_hash当我尝试auth_hash时,返回以下错误:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}在这里,我不知道为什么我指定了无效的凭据。
ENV['GOOGLE_API_KEY']指向与网站相同的API,而ENV['GOOGLE_CLIENT_API_SECRET']指向iOS客户端的密钥。
发布于 2012-08-13 10:32:06
我想,我上面使用的URL给了我一个access token。实际上,它返回给我一个authorization code (然后可以用它来获得一个access token)。
解决方案是将authorization code交换为access token,然后我可以传递令牌并使用它。
在谷歌网站上,authorization code和access token之间的区别有些模糊,但你可以在这里阅读到如何交换它们:https://developers.google.com/accounts/docs/OAuth2WebServer
https://stackoverflow.com/questions/11770995
复制相似问题