Hi,
I was trying to store the IAuthorizationState object while user accept the app using oauth. But when trying to refresh it it's not refreshing later and throws the exception. Here is the code:
code that stores IAuthorizationState object:
string authCode = value;
consumer.ClientCredentialApplicator =
ClientCredentialApplicator.PostParameter(clientSecret);
IAuthorizationState grantedAccess =
consumer.ProcessUserAuthorization(authCode);
string accessToken = grantedAccess.AccessToken;
GoogleApi api = new GoogleApi(accessToken);
string user = api.GetEmail();
api.RevokeToken(accessToken);
string json = JsonConvert.SerializeObject(
grantedAccess,
Formatting.Indented,
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects,
TypeNameAssemblyFormat =FormatterAssemblyStyle.Simple
});
File.WriteAllText(path + @"\Diduce\" + user + ".token", json);
imap.ConnectSSL("imap.gmail.com");
imap.LoginOAUTH2(user, accessToken);
Then when on next day I'm trying to refresh the token, it's throwing error.
Here is the code to get refreshed token:
string jsonString = System.IO.File.ReadAllText(
path
+ @"\Diduce\"
+ System.IO.File.ReadAllText(path + @"\Diduce\tmp1.sv")
+ ".token");
IAuthorizationState grantedAccess =
JsonConvert.DeserializeObject<IAuthorizationState>(
jsonString,
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Objects
}
);
//String str=grantedAccess.RefreshToken;
consumer.RefreshAuthorization(grantedAccess, TimeSpan.FromMinutes(90));
string accessToken = grantedAccess.RefreshToken;
GoogleApi api = new GoogleApi(accessToken);
string user = api.GetEmail();
imap.ConnectSSL("imap.gmail.com");
imap.LoginOAUTH2(user, accessToken);
So it's throwing exception at "consumer.RefreshAuthorization(grantedAccess, TimeSpan.FromMinutes(90));"
Please help me to refresh the oauth token without asking user several time to click on accept button.