OAuth2を使用してWindowsPhone7のGoogleAPIでいくつかの作業を行っています。
APIを呼び出す前に、アクセストークンを更新する必要があります(必要な場合)。
私が抱えている問題は、APIの呼び出しを続行する前に、トークンの更新が完了する非同期リクエストを待っていることです。
以下のコードで動作しますが、満足していません。whileループで変数を単純にポーリングするよりも良い代替手段を探しています。
/// <summary>
/// Calls the API.
/// </summary>
/// <param name="callback">The callback.</param>
/// <param name="api">The API.</param>
/// <param name="queryStrings">The query strings.</param>
public static void CallApi(AsyncCallback callback, string api, Dictionary<string, string> queryStrings = null)
{
var uri = new Uri(string.Format("{0}{1}{2}", ApiBaseUrl, api, queryStrings.ToQueryString()));
if (accessTokenExpires < DateTime.Now.AddSeconds(300))
{
RefreshAccessToken();
}
// Start new thread so we don't lock up the UI thread while waiting
var thread = new Thread(
() =>
{
// Wait for the access token to be updated
while (accessTokenExpires < DateTime.Now.AddSeconds(300)) ;
var request = WebRequest.CreateHttp(uri);
request.Headers["Authorization"] = string.Format("OAuth {0}", accessToken);
request.BeginGetResponse(callback, request);
});
thread.Start();
}
/// <summary>
/// Refresh the access token.
/// </summary>
private static void RefreshAccessToken()
{
if (Config.GoogleOAuthRefreshToken == string.Empty)
{
// TODO - Present the OAuth popup to the user
return;
}
var uri = new Uri(GetTokenUrl);
var request = WebRequest.CreateHttp(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(RefreshToken_GetRequest, request);
}