Google通讯录API OAuth2问题

时间:2015-02-24 07:38:04

标签: c# google-api google-contacts google-data-api google-oauth2

我是工作管理员的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);
        }
    }
}
大家能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,

这是我的代码:

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);
        }
    }
}