使用 Apache HttpClient 调用 Google auth API
call Google auth API using Apache HttpClient
我想知道是否可以使用 Apache HttpClient 调用需要身份验证的 Google API,例如 Google 日历 API,以及没有 Google 代码或库。 (并且需要代码来完成)
这是我目前的代码,它遇到了一个验证错误,
user/password用什么?
HttpPost request = new HttpPost("https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?key=mykey");
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY),
new UsernamePasswordCredentials(user, password));
HttpResponse response = client.execute(request);
错误:
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
您不使用登录名和密码,您需要通过身份验证,一旦您获得访问令牌,您就可以调用类似的方法。
https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?access_token={tokenFromAuth}
您可以使用支持 HTTP POST 和 HTTP GET 的任何语言向 Google 进行身份验证。这是我对 Google http://www.daimto.com/google-3-legged-oauth2-flow/ 的身份验证流程的介绍,您需要在 Google 开发人员控制台上创建 Oauth2 凭据才能开始。然后,只需询问用户是否允许访问其数据并请求访问即可。
我想知道是否可以使用 Apache HttpClient 调用需要身份验证的 Google API,例如 Google 日历 API,以及没有 Google 代码或库。 (并且需要代码来完成)
这是我目前的代码,它遇到了一个验证错误, user/password用什么?
HttpPost request = new HttpPost("https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?key=mykey");
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY),
new UsernamePasswordCredentials(user, password));
HttpResponse response = client.execute(request);
错误:
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
您不使用登录名和密码,您需要通过身份验证,一旦您获得访问令牌,您就可以调用类似的方法。
https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?access_token={tokenFromAuth}
您可以使用支持 HTTP POST 和 HTTP GET 的任何语言向 Google 进行身份验证。这是我对 Google http://www.daimto.com/google-3-legged-oauth2-flow/ 的身份验证流程的介绍,您需要在 Google 开发人员控制台上创建 Oauth2 凭据才能开始。然后,只需询问用户是否允许访问其数据并请求访问即可。