[英]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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.