想用dotnetOpenAuth请求gmail授权
问题描述:
我用dotnetOpenAuth。我想向用户的gamil请求授权。想用dotnetOpenAuth请求gmail授权
我需要先使用openId吗?
找不到像样的tutorail。谁能帮忙?
尝试此代码unsuccesfully。反正我不似乎要求在身份验证请求Gmail的范围,所以我很困惑
public void PrepareAuthorizationRequest(Uri authCallbakUrl)
{
var consumer = new WebConsumer(GoogleConsumerConsts.ServiceDescription, mConsumerTokenManager);
// request access
consumer.Channel.Send(consumer.PrepareRequestUserAuthorization(authCallbakUrl, null, null));
throw new NoRedirectToAuthPageException();
}
public ProcessAuthorizationRequestResponse ProcessAuthorizationRequest()
{
ProcessAuthorizationRequestResponse response;
// Process result from the service provider
var consumer = new WebConsumer(GoogleConsumerConsts.ServiceDescription, mConsumerTokenManager);
var accessTokenResponse = consumer.ProcessUserAuthorization();
// If we didn't have an access token response, this wasn't called by the service provider
if (accessTokenResponse == null)
response = new ProcessAuthorizationRequestResponse
{
IsAuthorized = false
};
else
{
// Extract the access token
string accessToken = accessTokenResponse.AccessToken;
response = new ProcessAuthorizationRequestResponse
{
IsAuthorized = true,
Token = accessToken,
Secret = mConsumerTokenManager.GetTokenSecret(accessToken)
};
}
return response;
}
private string Test2()
{
// Process result from linked in
var google = new WebConsumer(GoogleConsumerConsts.ServiceDescription, mConsumerTokenManager);
// var accessToken = GetAccessTokenForUser();
var accessToken = String.Empty;
// Retrieve the user's profile information
var endpoint = GoogleConsumerConsts.GetGmailFeedsEndpoint;// new MessageReceivingEndpoint("http://api.linkedin.com/v1/people/~", HttpDeliveryMethods.GetRequest);
var request = google.PrepareAuthorizedRequest(endpoint, accessToken);
var response = request.GetResponse();
return (new StreamReader(response.GetResponseStream())).ReadToEnd();
}
答
不,你并不需要使用OpenID的,如果你只是想访问用户的Gmail。 OpenID适用于您想要验证用户的情况。 OAuth用于访问用户数据。
您需要在您的授权请求中包含scope参数,如此问题所述:Adding scopes to OAuth 1.0 authorization request with DotNetOpenAuth。
如果我不使用openAuth,我应该在数据库中保存什么,以便下次识别用户? requestToken是永久且唯一的吗?第二次使用者的流量是多少? – 2012-03-04 22:26:06
请求令牌或访问令牌不是持久的。如果您希望能够在下次识别用户时,可以使用OpenID *(除了OAuth之外还可以实际阅读他们的Gmail)。在这种情况下,您应该查看'openidrelyingpartywebforms/loginplusoauth.aspx'示例,这些示例使用Google *登录用户,以及*访问其Gmail地址簿。 – 2012-03-05 01:06:57
它会导致用户双重重定向?一次用于身份验证,一次用于授权? – 2012-03-05 19:00:15