1

我想知道是否可以使用Apache HttpClient调用需要身份验证的Google API,例如Google Calendar API,并且不需要Google代码或库。 (并且需要代码去做)使用Apache HttpClient调用Google auth API

这是我到目前为止的代码,它得到一个认证错误, 你对用户/密码有什么用处?

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" 
+0

注意这是一个非用户交互过程,它需要每隔一小时检查一次日历并且没有任何过期的凭据 – James

回答

1

你不使用登录名和你需要密码一旦你,你将有一个访问令牌,那么你可以调用像这样进行身份验证。

https://www.googleapis.com/calendar/v3/users/me/calendarList/primary?access_token= {} tokenFromAuth

您可以用能够在HTTP POST的任何语言和HTTP GET到谷歌验证。以下是我对Google http://www.daimto.com/google-3-legged-oauth2-flow/的身份验证流程的步骤,您需要在Google Developer Console上创建Oauth2凭据才能开始。然后它只是要求用户访问他们的数据和请求访问的权限。

+0

没有用户,它是一个bot吗?用户需要在机器人的配置中输入他们的凭证,而不是用户交互式进程 – James

+0

Google不再支持客户端登录。您的用户至少需要验证一次,然后您的机器人才能自动运行。但是,某些apis支持服务帐户的代码包括创建jwt令牌难以开展工作,用户必须在Google Developer Consol上创建自己的项目并预先批准他们想要访问的任何数据 – DaImTo

+0

用户验证声明令牌只持续1小时? – James

相关问题