我有一个控制台应用程序,它使用 YouTube V3 API 提取视频视图计数数据。我正在使用 Oauth 2 进行身份验证并遇到问题。
我在不同的时间在两台不同的服务器上执行了我的应用程序。
一台机器成功验证并从日志中返回以下消息:
2013-05-08 09:51:45,691 [8] INFO DotNetOpenAuth.Messaging.Channel - Prepared outgoing AccessTokenRefreshRequest (2.0) message for https://accounts.google.com/o/oauth2/token:
refresh_token: 1/m4PJx978kbEGkQTf6tvSJdFCzej1Na6NBTakhhHQKjs
grant_type: refresh_token
client_id: 1085346326111.apps.googleusercontent.com
client_secret: HHH5WFqTRdXMl54rNZaUghNa
另一个没有成功验证,我假设它正在等待代表用户提交 YouTube 数据 API 请求的权限:
2013-05-08 09:44:59,442 [31] 信息 DotNetOpenAuth.Messaging.Channel - 为https://accounts.google.com/o/oauth2/auth准备传出 EndUserAuthorizationRequest (2.0) 消息:response_type:代码 client_id:1085346326111 .apps.googleusercontent.com redirect_uri:服务器本地主机路径范围:https ://www.googleapis.com/auth/youtube.readonly
谁能给我一个解释,为什么应用程序可能无法根据两台服务器使用相同的代码、相同的 clientID 和相同的密钥这一事实在一台服务器上进行身份验证?
这是我的代码片段:
私人无效Oauth2Authenticate(){
try
{
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
{
ClientIdentifier = ConfigurationManager.AppSettings["Client.Id"],
ClientSecret = ConfigurationManager.AppSettings["Client.Secret"]
};
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
youtube = new YoutubeService(new BaseClientService.Initializer()
{
Authenticator = auth
});
}
catch (Exception e)
{
if (logger != null)
logger.Error(string.Format("Error authenticating Aplication on YouTube authentiction server", e.Message), e);
}
}
private static IAuthorizationState GetAuthorization(NativeApplicationClient client)
{
var storage = MethodBase.GetCurrentMethod().DeclaringType.ToString();
var key = "storage_key";
IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(storage, key);
if (state != null)
{
client.RefreshToken(state);
}
else
{
state = AuthorizationMgr.RequestNativeAuthorization(client, YoutubeService.Scopes.YoutubeReadonly.GetStringValue());
AuthorizationMgr.SetCachedRefreshToken(storage, key, state);
}
return state;
}