如何在google应用程序引擎端点中获取User对象的访问令牌



我可以获得用户的谷歌信息,如电子邮件和昵称。但我想从作用域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;
    }

}

不能,appengine中的用户对象是获取当前已验证用户的一种方便快捷的方法,这在appengine中将被维护为Principle。

如果你想获得访问令牌,你必须使用Google OAuth2,如果用户同意,你将获得访问令牌。

相关内容

  • 没有找到相关文章

最新更新