Google Calendar API v3-使用硬编码的凭据进行身份验证

Google Calendar API v3-使用硬编码的凭据进行身份验证,第1张

Google Calendar API v3-使用硬编码的凭据进行身份验证

您将需要同时使用开发者密钥(API密钥)和OAuth2。开发人员密钥对谁编写软件进行身份验证,并用于配额之类的事情,配额是基于每个开发人员而不是每个用户的。OAuth2用于用户身份验证,将需要访问非公共日历。

OAuth2具有续订令牌,您可以从中生成会话令牌,这意味着您无需屏幕刮OAuth屏幕即可进行身份验证。为此,我将编写一个小的命令行应用程序,或者使用一个关闭的PHP页面。

  1. 在Google Api控制台下,转到API访问权限
  2. 生成一个新的客户端ID并选择Installed Application(已安装的应用程序)(因为您将以不作为用户的身份验证服务器)
  3. 使用控制台应用程序或一个关闭的PHP页面,使用OAuth和您的Google帐户(带有您要访问的日历的帐户)进行身份验证
  4. 在认证返回中,应该有一个续订令牌(称为续订或刷新或类似的东西)。保存此字符串,并将其提供给您的PHP站点。
  5. 当您需要访问服务时,您的OAuth库应进行续订/刷新调用。下面是使用.Net的示例。

private IAuthorizationState CreateAuthorization(NativeApplicationClient arg) {   // Get the auth URL:   IAuthorizationState state = new AuthorizationState(new[] { AdsenseService.Scopes.AdsenseReadonly.GetStringValue() });   state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);   if (refreshToken.IsNotNullOrEmpty()) // refreshToken you stored in step 4   {     try     {       state.RefreshToken = refreshToken;       if (arg.RefreshToken(state))     // This is calling out to the OAuth servers with the refresh token getting back a session token, returns true if successful.       {         if (state.RefreshToken != refreshToken) // if the refresh token has changed, save it.         {PersistRefreshToken(authorization.RefreshToken);         }         return this.authorization = state; // Retain the authorization state, this is what will authenticate your calls.       }     }     catch (ProtocolException ex) {...}

现在可以将已更新的AuthorizationState用来验证对API的调用。此状态可以使用很多次,直到到期,然后可以刷新。当您以自己(而非用户)身份验证应用程序时,所有会话都可以共享此AuthorizationState。当前的AuthorizationState和刷新令牌都应安全地保存在您的服务器上,并且永远不要发送给客户端,如果您将它们作为响应的一部分发送,则您的客户端将具有与代码应用程序相同的特权



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5585954.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-15
下一篇2022-12-14

发表评论

登录后才能评论

评论列表(0条)

    保存