0 votes

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.

by (1.4k points)

1 Answer

0 votes

1.
You are revoking the token:

api.RevokeToken(accessToken);

this causes you are not able to refresh it later on.

2.
Another problem in my opinion is here:

string accessToken = grantedAccess.RefreshToken;

RefreshToken is not an access token. Use grantedAccess.AccessToken.

by (297k points)
I have changed according to your suggestions.
I removed the line api.RevokeToken(accessToken);
and also changed accessToken as string accessToken = grantedAccess.AccessToken;

Still I'm getting error while refreshing the token at line "consumer.RefreshAuthorization(grantedAccess, TimeSpan.FromMinutes(90));".

Here is the error message:
{"Error occurred while sending a direct message or getting the response."}
Are you sure that IAuthorizationState is correctly deserialized? Try turning on DNOA logging. Is the grantedAccess.RefreshToken value not null?
Yes it is deserialized correctly.
and refreshtoken value is not null after getting refresh token.

And i don't have any idea about DNOA logging.
Your problem is with DotNetOpenAuth (DNOA), not Mail.dll itself. With such limited error message, it's really hard to help you. I have no problems refreshing the access token. Does RefreshAuthorization works for you without serialization (just after you obtained it)?
...