Google 联系人 API OAuth2 问题
Google Contacts API OAuth2 Issue
我是 Google 工作管理员的应用程序,
我正在编写服务器到服务器应用程序以从我的域帐户获取联系人信息,
而且我可以自己获取联系人信息,
但是当我访问我的域帐户用户时出现 403 代码错误。
这是我的代码:
public class BESGoogleContactsService
{
private const string serviceAccountEmail = "123123-123@developer.gserviceaccount.com";
private const string serviceAccountCertPath = "BesSSO-123123.p12";
private const string serviceAccountCertPassword = "notasecret";
private const string adminEmail = "admin@domain.com";
public BESGoogleContactsService()
{
var certificate = new X509Certificate2(serviceAccountCertPath, serviceAccountCertPassword, X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { "https://www.google.com/m8/feeds/" },
User = adminEmail
}.FromCertificate(certificate));
bool success = credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result;
RequestSettings settings =
new RequestSettings("Google Sync.", credential.Token.AccessToken)
{
AutoPaging =true,
UseSSL = true
};
ContactsRequest cr = new ContactsRequest(settings);
PrintAllContacts(cr);
}
public static void PrintAllContacts(ContactsRequest cr)
{
Feed<Contact> feed = cr.GetContacts("225402@domain.com");
Console.WriteLine(feed.TotalResults);
foreach (Contact entry in feed.Entries)
{
Console.WriteLine(entry.Name.FullName);
}
}
}
大家可以帮我解决这个问题吗?
我解决了这个问题,
这是我的代码:
public class BESGoogleContactsService
{
private const string serviceAccountEmail = "123-123123@developer.gserviceaccount.com";
private const string serviceAccountCertPath = "BesSSO-123.p12";
private const string serviceAccountCertPassword = "notasecret";
private const string Email = "225342@domain.com";
public BESGoogleContactsService()
{
var certificate = new X509Certificate2(serviceAccountCertPath, serviceAccountCertPassword, X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { "https://www.google.com/m8/feeds/" },
User = Email
}.FromCertificate(certificate));
bool success = credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result;
RequestSettings settings =
new RequestSettings("Google Sync.", credential.Token.AccessToken)
{
AutoPaging =true,
UseSSL = true
};
ContactsRequest cr = new ContactsRequest(settings);
PrintAllContacts(cr);
}
public static void PrintAllContacts(ContactsRequest cr)
{
Feed<Contact> feed = cr.GetContacts(Email);
Console.WriteLine(feed.TotalResults);
foreach (Contact entry in feed.Entries)
{
Console.WriteLine(entry.Name.FullName);
}
}
}
我是 Google 工作管理员的应用程序,
我正在编写服务器到服务器应用程序以从我的域帐户获取联系人信息,
而且我可以自己获取联系人信息,
但是当我访问我的域帐户用户时出现 403 代码错误。
这是我的代码:
public class BESGoogleContactsService
{
private const string serviceAccountEmail = "123123-123@developer.gserviceaccount.com";
private const string serviceAccountCertPath = "BesSSO-123123.p12";
private const string serviceAccountCertPassword = "notasecret";
private const string adminEmail = "admin@domain.com";
public BESGoogleContactsService()
{
var certificate = new X509Certificate2(serviceAccountCertPath, serviceAccountCertPassword, X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { "https://www.google.com/m8/feeds/" },
User = adminEmail
}.FromCertificate(certificate));
bool success = credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result;
RequestSettings settings =
new RequestSettings("Google Sync.", credential.Token.AccessToken)
{
AutoPaging =true,
UseSSL = true
};
ContactsRequest cr = new ContactsRequest(settings);
PrintAllContacts(cr);
}
public static void PrintAllContacts(ContactsRequest cr)
{
Feed<Contact> feed = cr.GetContacts("225402@domain.com");
Console.WriteLine(feed.TotalResults);
foreach (Contact entry in feed.Entries)
{
Console.WriteLine(entry.Name.FullName);
}
}
}
大家可以帮我解决这个问题吗?
我解决了这个问题,
这是我的代码:
public class BESGoogleContactsService
{
private const string serviceAccountEmail = "123-123123@developer.gserviceaccount.com";
private const string serviceAccountCertPath = "BesSSO-123.p12";
private const string serviceAccountCertPassword = "notasecret";
private const string Email = "225342@domain.com";
public BESGoogleContactsService()
{
var certificate = new X509Certificate2(serviceAccountCertPath, serviceAccountCertPassword, X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { "https://www.google.com/m8/feeds/" },
User = Email
}.FromCertificate(certificate));
bool success = credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result;
RequestSettings settings =
new RequestSettings("Google Sync.", credential.Token.AccessToken)
{
AutoPaging =true,
UseSSL = true
};
ContactsRequest cr = new ContactsRequest(settings);
PrintAllContacts(cr);
}
public static void PrintAllContacts(ContactsRequest cr)
{
Feed<Contact> feed = cr.GetContacts(Email);
Console.WriteLine(feed.TotalResults);
foreach (Contact entry in feed.Entries)
{
Console.WriteLine(entry.Name.FullName);
}
}
}