Verwenden Sie die Verzeichnis-API von Google für .Net?
Ich versuche, meine erste Konsolen-App mit der Google Directory API für .Net zu erstellen.
Ich habe einen Code, der auf einem Google-Beispiel basiert. Es zeigt mir ein paar Fehler,einer von ihnen ist, wenn ich versuche, den Dienst zu erstellen:
var service = new DirectoryService(new BaseClientService.Initializer()
{
Authenticator = auth,
ApplicationName = "Create User",
ApiKey = "<your API Key from Google APIs console>"
});
Es zeigt mir: "Fehler 3 'Google.Apis.Services.BaseClientService.Initializer' enthält keine Definition für 'Authenticator'"
Und daszweite Fehler ist in dieser Funktion
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg){}
Es zeigt mir: "DotNetO, penAuth.OAuth2.UserAgentClient 'ist in einer Assembly definiert, auf die nicht verwiesen wird."
In diesem Fall habe ich Folgendes eingegeben (mit der Nuget-Konsole): PM> Install-Package DotNetOpenAuth -Version 4.3.4.13329 .... aber das Problem ist damit nicht behoben.
Das ist mein Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
//using Google.Apis.Samples.Helper;
using Google.Apis.Services;
using Google.Apis.Util;
using Google.Apis.Admin.Directory.directory_v1;
using Google.Apis.Admin.Directory.directory_v1.Data;
namespace GoogleDirectoryApi_test02_consola
{
class Program
{
static void Main(string[] args)
{
String CLIENT_ID = "YOUR_CLIENT_ID";
String CLIENT_SECRET = "YOUR_CLIENT_SECRET";
// Register the authenticator and create the service
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
//New User
User newuserbody = new User();
string userId = "SampleId01";
UserName newusername = new UserName();
newuserbody.PrimaryEmail = userId;
// Create the service.
var service = new DirectoryService(new BaseClientService.Initializer()
{
Authenticator = auth,
ApplicationName = "Create User",
ApiKey = "<your API Key from Google APIs console>"
});
User results = service.Users.Insert(newuserbody).Execute();
}
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
// Get the auth URL:
//IAuthorizationState state = new AuthorizationState(new[] { DirectoryService.Scopes.AdminDirectoryUser.GetStringValue() });
IAuthorizationState state = new AuthorizationState(new[] { DirectoryService.Scope.AdminDirectoryUser.ToString() });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
Uri authUri = arg.RequestUserAuthorization(state);
// Request authorization from the user (by opening a browser window):
Process.Start(authUri.ToString());
Console.WriteLine();
Console.Write("Authorization Code: ");
string authCode = Console.ReadLine();
// Retrieve the access token by using the authorization code:
return arg.ProcessUserAuthorization(authCode, state);
}
}
}
Vielen Dank im Voraus für Ihre Hilfe