【问题标题】:How to get the access token in google app engine endpoints for User object?如何在谷歌应用引擎端点中获取用户对象的访问令牌?
【发布时间】:2016-07-14 21:31:25
【问题描述】:

我能够获取用户 Google 信息,例如电子邮件和昵称。但我想从https://www.googleapis.com/auth/userinfo.profile范围内检索用户个人资料图片

要访问它,我需要拨打此电话
https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=XXX

这将返回包含个人资料图片链接的 Json 数据。 带有 access_token 的上述请求的示例响应

{
  "id": "123",
  "name": "Admin User",
  "given_name": "Admin",
  "family_name": "User",
  "link": "https://plus.google.com/XYZ",
  "picture": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/XYX/4252rscbv5M/photo.jpg",
  "locale": "en"
}

如何在 Java 中获取访问令牌?

我的代码贴在下面:

package com.example.myapplication.backend;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.appengine.api.oauth.OAuthRequestException;
import com.google.appengine.api.users.User;

import java.io.IOException;

/** An endpoint class we are exposing */
@Api(
  name = "myApi",
  version = "v1",

        scopes = { Constants.EMAIL_SCOPE2 },
        clientIds = { Constants.WEB_CLIENT_ID, Constants.ANDROID_CLIENT_ID,
                Constants.API_EXPLORER_CLIENT_ID},
        audiences = {Constants.ANDROID_AUDIENCE},


  namespace = @ApiNamespace(
    ownerDomain = "backend.myapplication.Ajay.example.com",
    ownerName = "backend.myapplication.Ajay.example.com",
    packagePath=""
  )
)
public class MyEndpoint {

    /** A simple endpoint method that takes a name and says Hi back */
    @ApiMethod(name = "sayHi")
    public MyBean sayHi( User user) throws OAuthRequestException, IOException
     {
        MyBean response = new MyBean();
         response.setData(user.getEmail() + "  nick name->" + user.getNickname() + "user id " + user.getUserId() + "-->" +
                 user.toString() + "getAuthDomain--->" + user.getAuthDomain().toString()+" token");
        return response;
    }


}

【问题讨论】:

  • 我们不能在服务器端执行此操作,除非我们有用户的刷新令牌。而是在客户端执行此操作

标签: java google-app-engine oauth-2.0 google-api access-token


【解决方案1】:

你不能,appengine 中的 user 对象是获取当前认证用户的一种方便快捷的方式,这在 appengine 中被维护为 Principle。

如果您想获得访问令牌,您必须使用Google OAuth2,然后如果用户批准,您将获得访问令牌。

【讨论】:

    猜你喜欢
    • 2015-10-06
    • 2014-05-03
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 2017-11-13
    • 2013-07-20
    相关资源
    最近更新 更多