我正在为我的asp.net应用程序使用Google日历版本3(1.5.0.56-beta)测试版,现在我想将这些DLL升级到版本3(1.8.1.820)。我已经下载了新的DLL,但它没有显示NativeApplicationClient,GoogleAuthenticationServer不在这些DLL中。请检查我用于V3 beta版的以下代码
private CalendarService CreateService(string token)
{
KeyValuePair<string, string> credentials = Common.Get3LOCredentials();
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier = credentials.Key;
provider.ClientSecret = credentials.Value;
var auth = new Google.Apis.Authentication.OAuth2.OAuth2Authenticator<NativeApplicationClient>(provider, (p) => GetAuthorization(provider, token, credentials.Key, credentials.Value));
CalendarService service = new CalendarService(new BaseClientService.Initializer()
{
Authenticator = auth,
ApiKey = ConfigurationManager.AppSettings["APIkey"].ToString(),
GZipEnabled = false
});
provider = null;
return service;
}
private IAuthorizationState GetAuthorization(NativeApplicationClient arg, String Refreshtoken, string clientid, string clientsecret)
{
int retrycount = 0;
IAuthorizationState state = null;
string accesstoken = string.Empty;
while (retrycount < 7)
{
accesstoken = string.Empty;
try
{
state = new AuthorizationState(new[] { CalendarService.Scopes.Calendar.GetStringValue() });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);// NativeApplicationClient.OutOfBandCallbackUrl);
ProfileSettingsBL bl = BLFactory.CurrentInstance.ProfileSettingsBLObj;
ProfileSettingsDS.SOUserCalendarAccountRow row = bl.RetrieveSOUserCalendarAccountByToken(Refreshtoken);
if (row != null)
{
string[] splitter = new string[1];
splitter[0] = Constants.EQUAL_REPLACE_STRING;
string[] arr = row.Token.Split(splitter, StringSplitOptions.None);
if (arr.Length == 1)
{
accesstoken = ExchangeCodeWithAccessAndRefreshToken(Refreshtoken, clientid, clientsecret, retrycount);
if (!string.IsNullOrEmpty(accesstoken))
{
string[] arr1 = accesstoken.Split(splitter, StringSplitOptions.None);
if (arr1.Length > 1)
{
bl.updateAccessToken(Refreshtoken, arr1[0], arr1[1]);
accesstoken = arr1[0];
}
}
}
else
{
if (arr.Length >= 1)
accesstoken = arr[1];
}
}
else
{
string[] splitter = new string[1];
splitter[0] = Constants.EQUAL_REPLACE_STRING;
accesstoken = ExchangeCodeWithAccessAndRefreshToken(Refreshtoken, clientid, clientsecret, retrycount);
if (!string.IsNullOrEmpty(accesstoken))
{
string[] arr1 = accesstoken.Split(splitter, StringSplitOptions.None);
if (arr1.Length >= 1)
accesstoken = arr1[0];
}
}
if (!string.IsNullOrEmpty(accesstoken))
state.AccessToken = accesstoken;
state.RefreshToken = Refreshtoken;
return state;
}
catch (Exception ex)
{
}
}
return state;
}
任何人都可以在这方面帮助我,无法为.NET开发人员找到好的文档。提前谢谢。
答案 0 :(得分:0)
现在我已将旧应用程序转换为新应用程序,在这里您只需要使用Google.Apis.Auth.OAuth2中的UserCredential类。请检查以下代码。
private CalendarService CreateService(string token)
{
KeyValuePair<string, string> credentials = Common.Get3LOCredentials();
String AccessToken = GetAuthorization(RefreshToken, credentials.Key, credentials.Value);
var tokenCode = new TokenResponse() { RefreshToken = RefreshToken, AccessToken = AccessToken };
var secreat = new ClientSecrets() { ClientId = credentials.Key, ClientSecret = credentials.Value };
UserCredential credential = new UserCredential(new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = secreat
}), "user", tokenCode);
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Google Calendar API Sample",
});
return service;
}
谷歌在Beta版本中的代码实际上非常容易。