EDN Admin
Well-known member
Hi after running this code i am getting error.The type or namespace name UserInfo could not be found
using System;<br/>
using System.Diagnostics;<br/>
using DotNetOpenAuth.OAuth2;<br/>
using Google.Apis.Authentication.OAuth2;<br/>
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;<br/>
using Google.Apis.Samples.Helper;<br/>
using Google.Apis.Tasks.v1;<br/>
using Google.Apis.Tasks.v1.Data;<br/>
using Google.Apis.Util;<br/>
using Google.Apis.Authentication;<br/>
using System.Web;<br/>
<br/>
<br/>
<br/>
namespace Google.Apis.Samples.TasksOAuth2<br/>
{<br/>
/// <summary><br/>
/// This sample demonstrates the simplest use case for an OAuth2 service.
<br/>
/// The schema provided here can be applied to every request requiring authentication.<br/>
/// </summary><br/>
public class Program<br/>
{<br/>
public static void Main(string[] args)<br/>
{<br/>
// Display the header and initialize the sample.<br/>
CommandLine.EnableExceptionHandling();<br/>
CommandLine.DisplayGoogleSampleHeader("Tasks API");<br/>
<br/>
// Register the authenticator.<br/>
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);<br/>
provider.ClientIdentifier = "dsfsf.apps.dsfds.com";<br/>
provider.ClientSecret = "fsdsdfds";<br/>
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);<br/>
<br/>
// Create the service.<br/>
var service = new TasksService(auth);<br/>
TaskLists results = service.Tasklists.List().Fetch();<br/>
Console.WriteLine("Lists:");<br/>
foreach (TaskList list in results.Items)<br/>
{<br/>
Console.WriteLine("- " + list.Title);<br/>
}<br/>
Console.ReadKey();<br/>
}<br/>
<br/>
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)<br/>
{<br/>
// Get the auth URL:<br/>
IAuthorizationState state = new AuthorizationState(new[] { TasksService.Scopes.Tasks.GetStringValue() });<br/>
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);<br/>
Uri authUri = arg.RequestUserAuthorization(state);<br/>
<br/>
// Request authorization from the user (by opening a browser window):<br/>
Process.Start(authUri.ToString());<br/>
Console.Write(" Authorization Code: ");<br/>
string authCode = Console.ReadLine();<br/>
Console.WriteLine();<br/>
<br/>
// Retrieve the access token by using the authorization code:<br/>
return arg.ProcessUserAuthorization(authCode, state);<br/>
}<br/>
<br/>
//---------------------------------------------------<br/>
public static IAuthenticator GetCredentials(String authorizationCode, String state)<br/>
{<br/>
String emailAddress = "";<br/>
try<br/>
{<br/>
IAuthorizationState credentials = ExchangeCode(authorizationCode);<br/>
Userinfo userInfo = GetUserInfo(credentials);<br/>
String userId = userInfo.Id;<br/>
emailAddress = userInfo.Email;<br/>
if (!String.IsNullOrEmpty(credentials.RefreshToken))<br/>
{<br/>
StoreCredentials(userId, credentials);<br/>
return GetAuthenticatorFromState(credentials);<br/>
}<br/>
else<br/>
{<br/>
credentials = GetStoredCredentials(userId);<br/>
if (credentials != null &&<br/>
!String.IsNullOrEmpty(credentials.RefreshToken))<br/>
{<br/>
return GetAuthenticatorFromState(credentials);<br/>
}<br/>
}<br/>
}<br/>
catch (CodeExchangeException e)<br/>
{<br/>
Console.WriteLine("An error occurred during code exchange.");<br/>
// Drive apps should try to retrieve the user and credentials for<br/>
// the current session.<br/>
// If none is available, redirect the user to the authorization URL.<br/>
e.AuthorizationUrl = GetAuthorizationUrl(emailAddress, state);<br/>
throw e;<br/>
}<br/>
catch (NoUserIdException)<br/>
{<br/>
Console.WriteLine("No user ID could be retrieved.");<br/>
}<br/>
// No refresh token has been retrieved.<br/>
String authorizationUrl = GetAuthorizationUrl(emailAddress, state);<br/>
throw new NoRefreshTokenException(authorizationUrl);<br/>
}<br/>
<br/>
/// <summary><br/>
/// Exchange an authorization code for OAuth 2.0 credentials.<br/>
/// </summary><br/>
/// <param name="authorizationCode Authorization code to exchange<br/>
//// for OAuth 2.0 credentials.</param><br/>
/// <returns>OAuth 2.0 credentials.</returns><br/>
/// <exception cref="CodeExchangeException An error occurred.</exception><br/>
static IAuthorizationState ExchangeCode(String authorizationCode)<br/>
{<br/>
var provider = new NativeApplicationClient(<br/>
GoogleAuthenticationServer.Description,<br/>
ClientCredentials.CLIENT_ID,<br/>
ClientCredentials.CLIENT_SECRET);<br/>
IAuthorizationState state = new AuthorizationState();<br/>
state.Callback = new Uri(ClientCredentials.REDIRECT_URI);<br/>
try<br/>
{<br/>
state = provider.ProcessUserAuthorization(authorizationCode, state);<br/>
return state;<br/>
}<br/>
catch (ProtocolException)<br/>
{<br/>
throw new CodeExchangeException(null);<br/>
}<br/>
}<br/>
<br/>
/// <summary><br/>
/// Send a request to the User Info API to retrieve the users information.<br/>
/// </summary><br/>
/// <param name="credentials OAuth 2.0 credentials to authorize<br/>
//// the request.</param><br/>
/// <returns>Users information.</returns><br/>
/// <exception cref="NoUserIdException An error occurred.</exception><br/>
static Userinfo GetUserInfo(IAuthenticator credentials)<br/>
{<br/>
Oauth2Service userInfoService = new Oauth2Service(credentials);<br/>
Userinfo userInfo = null;<br/>
try<br/>
{<br/>
userInfo = userInfoService.Userinfo.Get().Fetch();<br/>
}<br/>
catch (GoogleApiRequestException e)<br/>
{<br/>
Console.WriteLine("An error occurred: " + e.Message);<br/>
}<br/>
<br/>
if (userInfo != null && !String.IsNullOrEmpty(userInfo.Id))<br/>
{<br/>
return userInfo;<br/>
}<br/>
else<br/>
{<br/>
throw new NoUserIdException();<br/>
}<br/>
}<br/>
<br/>
/// <summary><br/>
/// Retrieve an IAuthenticator instance using the provided state.<br/>
/// </summary><br/>
/// <param name="credentials OAuth 2.0 credentials to use.</param><br/>
/// <returns>Authenticator using the provided OAuth 2.0<br/>
/// credentials</returns><br/>
public static IAuthenticator GetAuthenticatorFromState(<br/>
IAuthorizationState credentials)<br/>
{<br/>
var provider = new StoredStateClient(<br/>
GoogleAuthenticationServer.Description, ClientCredentials.CLIENT_ID,<br/>
ClientCredentials.CLIENT_SECRET, credentials);<br/>
var auth = new OAuth2Authenticator<StoredStateClient>(<br/>
provider, StoredStateClient.GetState);<br/>
auth.LoadAccessToken();<br/>
return auth;<br/>
}<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
}<br/>
}<br/>
<
salman
<br/>
<br/>
View the full article
using System;<br/>
using System.Diagnostics;<br/>
using DotNetOpenAuth.OAuth2;<br/>
using Google.Apis.Authentication.OAuth2;<br/>
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;<br/>
using Google.Apis.Samples.Helper;<br/>
using Google.Apis.Tasks.v1;<br/>
using Google.Apis.Tasks.v1.Data;<br/>
using Google.Apis.Util;<br/>
using Google.Apis.Authentication;<br/>
using System.Web;<br/>
<br/>
<br/>
<br/>
namespace Google.Apis.Samples.TasksOAuth2<br/>
{<br/>
/// <summary><br/>
/// This sample demonstrates the simplest use case for an OAuth2 service.
<br/>
/// The schema provided here can be applied to every request requiring authentication.<br/>
/// </summary><br/>
public class Program<br/>
{<br/>
public static void Main(string[] args)<br/>
{<br/>
// Display the header and initialize the sample.<br/>
CommandLine.EnableExceptionHandling();<br/>
CommandLine.DisplayGoogleSampleHeader("Tasks API");<br/>
<br/>
// Register the authenticator.<br/>
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);<br/>
provider.ClientIdentifier = "dsfsf.apps.dsfds.com";<br/>
provider.ClientSecret = "fsdsdfds";<br/>
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);<br/>
<br/>
// Create the service.<br/>
var service = new TasksService(auth);<br/>
TaskLists results = service.Tasklists.List().Fetch();<br/>
Console.WriteLine("Lists:");<br/>
foreach (TaskList list in results.Items)<br/>
{<br/>
Console.WriteLine("- " + list.Title);<br/>
}<br/>
Console.ReadKey();<br/>
}<br/>
<br/>
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)<br/>
{<br/>
// Get the auth URL:<br/>
IAuthorizationState state = new AuthorizationState(new[] { TasksService.Scopes.Tasks.GetStringValue() });<br/>
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);<br/>
Uri authUri = arg.RequestUserAuthorization(state);<br/>
<br/>
// Request authorization from the user (by opening a browser window):<br/>
Process.Start(authUri.ToString());<br/>
Console.Write(" Authorization Code: ");<br/>
string authCode = Console.ReadLine();<br/>
Console.WriteLine();<br/>
<br/>
// Retrieve the access token by using the authorization code:<br/>
return arg.ProcessUserAuthorization(authCode, state);<br/>
}<br/>
<br/>
//---------------------------------------------------<br/>
public static IAuthenticator GetCredentials(String authorizationCode, String state)<br/>
{<br/>
String emailAddress = "";<br/>
try<br/>
{<br/>
IAuthorizationState credentials = ExchangeCode(authorizationCode);<br/>
Userinfo userInfo = GetUserInfo(credentials);<br/>
String userId = userInfo.Id;<br/>
emailAddress = userInfo.Email;<br/>
if (!String.IsNullOrEmpty(credentials.RefreshToken))<br/>
{<br/>
StoreCredentials(userId, credentials);<br/>
return GetAuthenticatorFromState(credentials);<br/>
}<br/>
else<br/>
{<br/>
credentials = GetStoredCredentials(userId);<br/>
if (credentials != null &&<br/>
!String.IsNullOrEmpty(credentials.RefreshToken))<br/>
{<br/>
return GetAuthenticatorFromState(credentials);<br/>
}<br/>
}<br/>
}<br/>
catch (CodeExchangeException e)<br/>
{<br/>
Console.WriteLine("An error occurred during code exchange.");<br/>
// Drive apps should try to retrieve the user and credentials for<br/>
// the current session.<br/>
// If none is available, redirect the user to the authorization URL.<br/>
e.AuthorizationUrl = GetAuthorizationUrl(emailAddress, state);<br/>
throw e;<br/>
}<br/>
catch (NoUserIdException)<br/>
{<br/>
Console.WriteLine("No user ID could be retrieved.");<br/>
}<br/>
// No refresh token has been retrieved.<br/>
String authorizationUrl = GetAuthorizationUrl(emailAddress, state);<br/>
throw new NoRefreshTokenException(authorizationUrl);<br/>
}<br/>
<br/>
/// <summary><br/>
/// Exchange an authorization code for OAuth 2.0 credentials.<br/>
/// </summary><br/>
/// <param name="authorizationCode Authorization code to exchange<br/>
//// for OAuth 2.0 credentials.</param><br/>
/// <returns>OAuth 2.0 credentials.</returns><br/>
/// <exception cref="CodeExchangeException An error occurred.</exception><br/>
static IAuthorizationState ExchangeCode(String authorizationCode)<br/>
{<br/>
var provider = new NativeApplicationClient(<br/>
GoogleAuthenticationServer.Description,<br/>
ClientCredentials.CLIENT_ID,<br/>
ClientCredentials.CLIENT_SECRET);<br/>
IAuthorizationState state = new AuthorizationState();<br/>
state.Callback = new Uri(ClientCredentials.REDIRECT_URI);<br/>
try<br/>
{<br/>
state = provider.ProcessUserAuthorization(authorizationCode, state);<br/>
return state;<br/>
}<br/>
catch (ProtocolException)<br/>
{<br/>
throw new CodeExchangeException(null);<br/>
}<br/>
}<br/>
<br/>
/// <summary><br/>
/// Send a request to the User Info API to retrieve the users information.<br/>
/// </summary><br/>
/// <param name="credentials OAuth 2.0 credentials to authorize<br/>
//// the request.</param><br/>
/// <returns>Users information.</returns><br/>
/// <exception cref="NoUserIdException An error occurred.</exception><br/>
static Userinfo GetUserInfo(IAuthenticator credentials)<br/>
{<br/>
Oauth2Service userInfoService = new Oauth2Service(credentials);<br/>
Userinfo userInfo = null;<br/>
try<br/>
{<br/>
userInfo = userInfoService.Userinfo.Get().Fetch();<br/>
}<br/>
catch (GoogleApiRequestException e)<br/>
{<br/>
Console.WriteLine("An error occurred: " + e.Message);<br/>
}<br/>
<br/>
if (userInfo != null && !String.IsNullOrEmpty(userInfo.Id))<br/>
{<br/>
return userInfo;<br/>
}<br/>
else<br/>
{<br/>
throw new NoUserIdException();<br/>
}<br/>
}<br/>
<br/>
/// <summary><br/>
/// Retrieve an IAuthenticator instance using the provided state.<br/>
/// </summary><br/>
/// <param name="credentials OAuth 2.0 credentials to use.</param><br/>
/// <returns>Authenticator using the provided OAuth 2.0<br/>
/// credentials</returns><br/>
public static IAuthenticator GetAuthenticatorFromState(<br/>
IAuthorizationState credentials)<br/>
{<br/>
var provider = new StoredStateClient(<br/>
GoogleAuthenticationServer.Description, ClientCredentials.CLIENT_ID,<br/>
ClientCredentials.CLIENT_SECRET, credentials);<br/>
var auth = new OAuth2Authenticator<StoredStateClient>(<br/>
provider, StoredStateClient.GetState);<br/>
auth.LoadAccessToken();<br/>
return auth;<br/>
}<br/>
<br/>
<br/>
<br/>
<br/>
<br/>
}<br/>
}<br/>
<
salman
<br/>
<br/>
View the full article