2

GoogleOAuthにDotNetOpenAuthを使用しようとしているASP.NETMVCアプリがあります。サンプルのGoogleConsumerクラスを使用しており、認証の最初のステップを実行しようとしています。以下のコードは、MVCコントローラーだけで、提供されているWebFormsアプリケーションのコードと基本的に同じです。

public string Authenticate()
{
  GoogleTokenManager tokenManager = new GoogleTokenManager(ConsumerKey, ConsumerSecret);
  WebConsumer webConsumer = new WebConsumer(GoogleConsumer.ServiceDescription, tokenManager);
  GoogleConsumer.RequestAuthorization(webConsumer, GoogleConsumer.Applications.Gmail);
  return "";
}

コントローラにAJAXリクエストを送信するとコードが実行されますが、認証のためにGoogleページにリダイレクトされることはありません。

4

1 に答える 1

3

根本的な要求は、私が適切に処理していなかった302リダイレクト応答を返していました。私がより役立つと思ったのは、次のように、コントローラー内の別のアクションへのコールバックURLを指定することでした。

public ActionResult Authenticate()
{
  string callbackUrl = Request.Url.ToString().Replace("Authenticate", "OtherAction");
  Uri callback = new Uri(callbackUrl);

  WebConsumer webConsumer = new WebConsumer(GoogleConsumer.ServiceDescription, TokenManager);
  Dictionary<string, string> extraParameters = new Dictionary<string, string>();
  extraParameters.Add("scope", GoogleConsumer.GetScopeUri(GoogleConsumer.Applications.Gmail));

  UserAuthorizationRequest request = webConsumer.PrepareRequestUserAuthorization(callback, extraParameters, null);
  return webConsumer.Channel.PrepareResponse(request).AsActionResult();
}

public ActionResult OtherAction()
{
  // oauth_verifier, oauth_token are now in the RequestQueryString
}
于 2011-04-26T01:28:53.853 に答える