您将需要同时使用开发者密钥(API密钥)和OAuth2。开发人员密钥对谁编写软件进行身份验证,并用于配额之类的事情,配额是基于每个开发人员而不是每个用户的。OAuth2用于用户身份验证,将需要访问非公共日历。
OAuth2具有续订令牌,您可以从中生成会话令牌,这意味着您无需屏幕刮OAuth屏幕即可进行身份验证。为此,我将编写一个小的命令行应用程序,或者使用一个关闭的PHP页面。
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和刷新令牌都应安全地保存在您的服务器上,并且永远不要发送给客户端,如果您将它们作为响应的一部分发送,则您的客户端将具有与代码应用程序相同的特权