error occurred while sending a direct message
private void btnAuthorize_Click(object sender, EventArgs e)
{
try
{
// Register the authenticator. The Client ID and secret have to be copied from the API Access
// tab on the Google APIs Console.
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description);
FullClientCredentials credentials = new FullClientCredentials();
credentials.ClientId = data.appUser;
credentials.ClientSecret = data.appPass;
credentials.ApiKey = data.appAPI;
provider.ClientIdentifier = credentials.ClientId;
provider.ClientSecret = credentials.ClientSecret;
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
// Create the service. This will automatically call the previously registered authenticator.
service = new CalendarService(auth);
Events results = service.Events.List("primary").Fetch();
//if (auth.State == null)
// return;
}
catch (Exception ex) { if (state != null)txtAuth.Text = state.AccessToken; MessageBox.Show(ex.Message); }
}
IAuthorizationState state;
private IAuthorizationState GetAuthorization(NativeApplicationClient client)
{
// You should use a more secure way of storing the key here as
// .NET applications can be disassembled using a reflection tool.
const string STORAGE = "google.samples.dotnet.siteverification";
const string KEY = "y},drdzf11x9;87";
// Check if there is a cached refresh token available.
state = AuthorizationMgr.GetCachedRefreshToken(STORAGE, KEY);
if (state != null)
{
try
{
client.RefreshToken(state);
txtAuth.Text = state.AccessToken;
return state; // Yes - we are done.
}
catch (DotNetOpenAuth.Messaging.ProtocolException ex)
{
//CommandLine.WriteError("Using existing refresh token failed: " + ex.Message);
}
}
// Retrieve the authorization from the user.
state = AuthorizationMgr.RequestNativeAuthorization(client, Scope);
txtAuth.Text = state.AccessToken;
AuthorizationMgr.SetCachedRefreshToken(STORAGE, KEY, state);
return state;
}