0

I am a Google app for work administrator,

i am program server to server application to get an contacts information from my domain account,

and i can get contacts information by myself,

but i get the 403 code error, when i access my domain account users.

This is my code:

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

can everyone help me solve this problem?

CHANGYU
  • 1
  • 2
  • 1
    403 is a forbidden error, there are some answers for it -http://stackoverflow.com/questions/20372453/getting-a-403-forbidden-for-google-service-account – yogihosting Feb 24 '15 at 11:29
  • thank you for your answers, but i added UserAccountEmail, i get the seam problem. – CHANGYU Feb 25 '15 at 03:32

1 Answers1

0

I solved this problem,

this is my code:

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);
        }
    }
}
CHANGYU
  • 1
  • 2