Моя цель: Используя один клик и перенаправление, я хочу, чтобы пользователь зашел на мой сайт и дал мне авторизацию (accessToken) на его Gmail.Я думал использовать A (DNOA) для авторизации + обновления до accessToken.
Но DNOA мне не очень понятен, поэтому я использовал другую dll (http://www.matlus.com/oauth-c-library/) in B .
Затем я понял, что хочу, чтобы пользователь прошел аутентификацию +авторизовался во второй раз, когда он заходит на мой сайт, поэтому, не перенаправляя его снова на «сайт Х запрашивает разрешения для вашей страницы Gmail».
Я понимаю, что должен использовать и OpenID, и OAuth. Поэтому я использовал код в C .
Несмотря на все вышесказанное, я запутался и не уверен, какой код больше всего подходит для моих нужд.
Может быть, ни один из них не подходит? Как я могу это сделать?проверить их на localhost? (см. комментарий к коду в C )
Любой свет, который вы можете пролить, будет благодарен!
A:
public void PrepareAuthorizationRequest(Uri authCallbakUrl)
{
var consumer = new WebConsumer(GoogleConsumerConsts.ServiceDescription, mConsumerTokenManager);
var requestParameters = new Dictionary<string, string>
{
{"scope", "https://www.googleapis.com/auth/userinfo#email"}};
// request access
consumer.Channel.Send(consumer.PrepareRequestUserAuthorization(authCallbakUrl, requestParameters, 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;
}
B:
public void GetAuthorizeRequestToken(OAuthProviderTypes authType)
{
var consumer = mAuthorizationConsumerFactory.GetConsumer(authType);
requestToken = GetRequestToken(consumer);
AuthorizeRequestToken(requestToken, consumer);
}
public AccessToken UpgradeToAccessToken(OAuthProviderTypes authType, RequestToken requestToken)
{
var consumer = mAuthorizationConsumerFactory.GetConsumer(authType);
var oAuthConsumer = new OAuthConsumer();
var accessToken = oAuthConsumer.GetOAuthAccessToken(consumer.AccessTokenEndpoint, _realm, consumer.ConsumerKey, consumer.ConsumerSecret, consumer.Token, consumer.Verifier, requestToken.TokenSecret);
System.Web.HttpContext.Current.Response.Redirect("~/Authentication.htm?google");
// Google Only - This method will get the email of the authenticated user
//var responseText = oAuthConsumer.GetUserInfo("https://www.googleapis.com/userinfo/email", realm, consumerKey, consumerSecret, accessToken.Token, accessToken.TokenSecret);
return new AccessToken();
}
private RequestToken GetRequestToken(IConsumer consumer)
{
var oAuthConsumer = new OAuthConsumer();
var requestToken = oAuthConsumer.GetOAuthRequestToken(consumer.RequestTokenEndpoint, _realm,
consumer.ConsumerKey, consumer.ConsumerSecret,
consumer.RequestTokenCallback);
// PersistRequestToken(requestToken);
return requestToken;
}
private void AuthorizeRequestToken(RequestToken requestToken, IConsumer consumer)
{
System.Web.HttpContext.Current.Response.Redirect(consumer.AuthorizeTokenUrl + "?oauth_token=" + requestToken.Token);
}
C:
private IAuthenticationRequest GetGoogleRequest()
{
// Google requires that the realm and consumer key be equal,
// so we constrain the realm to match the realm in the web.config file.
// This does mean that the return_to URL must also fall under the key,
// which means this sample will only work on a public web site
// that is properly registered with Google.
// We will customize the realm to use http or https based on what the
// return_to URL will be (which will be this page).
var consumer = new WebConsumer(GoogleConsumerConsts.ServiceDescription, mConsumerTokenManager);
//Realm realm = "http://localhost:8976/";
Realm realm = System.Web.HttpContext.Current.Request.Url.Scheme + Uri.SchemeDelimiter + consumer.ConsumerKey + "/";
IAuthenticationRequest authReq = GoogleConsumerConsts.RelyingParty.CreateRequest(GoogleConsumerConsts.GoogleOPIdentifier, realm);
// Prepare the OAuth extension
string scope = GoogleConsumerConsts.GetScopeUri(GoogleConsumerConsts.Applications.Contacts);
consumer.AttachAuthorizationRequest(authReq, scope);
// We also want the user's email address
var fetch = new FetchRequest();
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
authReq.AddExtension(fetch);
return authReq;
}