问题
I am developing an application for Windows Phone 8.1 which will use Google Blogger Api. For authorization I am using the GoogleWebAuthorizationBroker.AuthorizeAsync() function and everything is alright with that, I can do everything with my blog (get the blog/post lists, edit, delete, add posts ...).
When my application opens at first it calls GoogleWebAuthorizationBroker.AuthorizeAsync() function and in case when the user is already logged in (previously authenticated and authorized) the function returns and redirects to start page where I put the list of blogs. This is fine for me.
My problem is: When the user is not logged in I want to open another page ('welcome' page) where I will have some basic info about my application and sign in button. When user clicks that button in that case only I want to call GoogleWebAuthorizationBroker.AuthorizeAsync() function which will redirect me to sign-in page. But I couldn't find method to check if the user is already logged in and only in that case show the 'welcome' page.
Here is how I am using GoogleWebAuthorizationBroker.AuthorizeAsync() function.
m_credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = "my_client_id",
ClientSecret = "my_client_secret"
},
new[] { BloggerService.Scope.Blogger },
"user", CancellationToken.None);
回答1:
Finally I found the solution of my problem and wanted to share it here it can be helpful for someone.
That's wonderful the Google API codes are open for everyone so I could dig inside the GoogleWebAuthorizationBroker.AuthorizeAsync() function and saw all steps it does. Here is the all steps that I could collect into one function
private static async Task<UserCredential> AuthorizeAsync()
{
var initializer = new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = Constants.ClientId,
ClientSecret = Constants.ClientSecret
},
Scopes = new[] { BloggerService.Scope.Blogger },
DataStore = new StorageDataStore()
};
var flow = new AuthorizationCodeFlow(initializer);
var codeReceiver = new AuthorizationCodeBroker();
var token = await flow.LoadTokenAsync("user", CancellationToken.None).ConfigureAwait(false);
if (token == null || (token.RefreshToken == null && token.IsExpired(flow.Clock)))
{
var redirectUri = codeReceiver.RedirectUri;
AuthorizationCodeRequestUrl codeRequest = flow.CreateAuthorizationCodeRequest(redirectUri);
var response = await codeReceiver.ReceiveCodeAsync(codeRequest, CancellationToken.None).ConfigureAwait(false);
if (string.IsNullOrEmpty(response.Code))
{
var errorResponse = new TokenErrorResponse(response);
throw new TokenResponseException(errorResponse);
}
token = await flow.ExchangeCodeForTokenAsync("user", response.Code, codeReceiver.RedirectUri,
CancellationToken.None).ConfigureAwait(false);
}
m_credential = new UserCredential(flow, "user", token);
return m_credential;
}
The steps collected from different functions sequentially called by GoogleWebAuthorizationBroker.AuthorizeAsync() function. From code we can see this line
if (token == null || (token.RefreshToken == null && token.IsExpired(flow.Clock)))
which checks if the user is already authenticated or not. If yes it goes the next step else it authenticates the user. So I could write this simple function for me:
public async Task<bool> IsUserAuthenticated()
{
var initializer = new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = Constants.ClientId,
ClientSecret = Constants.ClientSecret
},
Scopes = new[] { BloggerService.Scope.Blogger },
DataStore = new StorageDataStore()
};
var flow = new AuthorizationCodeFlow(initializer);
var codeReceiver = new AuthorizationCodeBroker();
var token = await flow.LoadTokenAsync("user", CancellationToken.None).ConfigureAwait(false);
if (token == null || (token.RefreshToken == null && token.IsExpired(flow.Clock)))
{ return false; }
else
{ return true; }
}
来源:https://stackoverflow.com/questions/27257590/check-if-user-is-already-logged-in