我想通过DotNetOpenAuth和the dotnet library for Google Tasks使用OAuth 2.0访问Google任务。如何交换access_token的授权码?
说实话,我对整个OAuth过程感到有点困惑,但无论如何。
我试图按照该操场的OAuth2经过我的应用程序相同的过程:即
- 用户点击链接到授权应用
- 应用程序使用DotNetOpenAuth构建一个请求,谷歌
- 用户将看到“我的应用程序想要...”屏幕并授权应用程序
- 浏览器重定向到callback_uri,授权码为
- 代码交换为ccess_token
- 访问令牌在随后的请求
我不担心刷新令牌或任何尚未使用。
所以我到第5步,卡住了。我只是不知道如何交换访问令牌的授权码。
我呼吁OAuthAuthenticator<T>
(谷歌任务LIB的一部分)的方法称为LoadAccessToken
这听起来像是正确的方法,但是这将导致以下错误:
The following required parameters were missing from the
DotNetOpenAuth.OAuth2.Messages.AccessTokenAuthorizationCodeRequest message: redirect_uri
但是你可以从我的代码看在拨打LoadAccessToken
之前,我正在设置回拨。
这里是我的代码:
public class AuthController : Controller
{
private const string clientId = "xxxx";
private const string secret = "xxxx";
public ActionResult Authenticate()
{
UserAgentClient consumer = new UserAgentClient(GoogleAuthenticationServer.Description, clientId, secret);
IAuthorizationState state = new AuthorizationState(new[] { TasksService.Scopes.Tasks.GetStringValue() });
state.Callback = new Uri(Url.Action("OAuthCallback","Auth",null,"http"));
var request = consumer.RequestUserAuthorization(state);
return Redirect(request.ToString());
}
public ActionResult OAuthCallback(string code)
{
UserAgentClient consumer = new UserAgentClient(GoogleAuthenticationServer.Description, clientId, secret);
OAuth2Authenticator<UserAgentClient> authenticator = new OAuth2Authenticator<UserAgentClient>(consumer, ProcessAuth);
IAuthorizationState state = new AuthorizationState(new[] { TasksService.Scopes.Tasks.GetStringValue() });
state.Callback = new Uri(Url.Action("OAuthSuccess", "Auth", null, "http"));
authenticator.LoadAccessToken();
return RedirectToAction("List","Home");
}
public ActionResult OAuthSuccess(string access_token)
{
Session["token"] = access_token;
return RedirectToAction("List", "Home");
}
private IAuthorizationState ProcessAuth(UserAgentClient arg)
{
var state = arg.ProcessUserAuthorization(Request.Url);
return state;
}
}
任何想法?
这看起来非常感谢! – 2013-03-25 17:20:12