无法在 android 中使用 google apis 从 google 获取用户个人资料
我正在尝试将谷歌集成到我的 Android 应用程序中。我正在使用 oauth 来检索请求令牌、访问令牌。我想要用户的名字、姓氏、屏幕名称、电子邮件。这就是我想要做的,
public static final String CLIENT_ID = "XXXX";
public static final String CLIENT_SECRET = "XXXX";
private static final String SCOPE = "https://www.googleapis.com/auth/userinfo.profile";
public static final String REQUEST_TOKEN_URL = "https://www.google.com/accounts/OAuthGetRequestToken";
public static final String ACCESS_TOKEN_URL = "https://www.google.com/accounts/OAuthGetAccessToken";
public static final String AUTHORIZE_URL = "https://www.google.com/accounts/OAuthAuthorizeToken";
我连接到谷歌,要求授予个人资料信息的访问权限。用户授予访问权限,然后返回我的应用程序。现在我正在尝试这样做,
DefaultHttpClient httpClient = new DefaultHttpClient();
String strUrl = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
HttpPost request = new HttpPost(strUrl);
以获取用户的个人资料。但作为回应我只得到
{
无数据。我尝试了很多方法来获取用户配置文件。请告诉我我哪里做错了。任何链接、示例代码将不胜感激。提前致谢。
I am trying to integrate google in my android app. I am using oauth for retriving request token, access token. I want User's FirstName, LastName, ScreenName, Email. Here is what I am trying to do,
public static final String CLIENT_ID = "XXXX";
public static final String CLIENT_SECRET = "XXXX";
private static final String SCOPE = "https://www.googleapis.com/auth/userinfo.profile";
public static final String REQUEST_TOKEN_URL = "https://www.google.com/accounts/OAuthGetRequestToken";
public static final String ACCESS_TOKEN_URL = "https://www.google.com/accounts/OAuthGetAccessToken";
public static final String AUTHORIZE_URL = "https://www.google.com/accounts/OAuthAuthorizeToken";
I get connected to google asking to grant access for profile information. User grants the access, comes back to my application. Now I am trying to do this,
DefaultHttpClient httpClient = new DefaultHttpClient();
String strUrl = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json";
HttpPost request = new HttpPost(strUrl);
to get the user's profile. But In response I get only
{
No data. I tried a lot to get the user profile. Plz do tell me where I am doing wrong. Any links, sample code will be appreciated. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 HttpGet 而不是 HttpPost。该请求必须是资源的 GET 请求,而不是 POST 请求。
Try using an HttpGet instead of HttpPost. The request needs to be a GET request for the resource, not a POST request.