2015-02-24 168 views
0

我是谷歌應用程序的工作管理員,谷歌聯繫人API的OAuth2問題

我是程序服務器到服務器的應用程序從我的域帳戶得到一個聯繫人的信息,

,我自己能站聯繫人信息,

但是當我訪問我的域帳戶用戶時,我收到了403代碼錯誤。

這是我的代碼:

public class BESGoogleContactsService 
{ 
    private const string serviceAccountEmail = "[email protected]"; 
    private const string serviceAccountCertPath = "BesSSO-123123.p12"; 
    private const string serviceAccountCertPassword = "notasecret"; 
    private const string adminEmail = "[email protected]"; 
    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("[email protected]"); 

     Console.WriteLine(feed.TotalResults); 

     foreach (Contact entry in feed.Entries) 
     { 
       Console.WriteLine(entry.Name.FullName); 
     } 
    } 
} 

每個人都可以幫我解決這個問題呢?

+1

403是被禁止的錯誤,也有一些答案吧-http://stackoverflow.com/questions/20372453/getting - 403,禁止爲谷歌服務帳戶 – yogihosting 2015-02-24 11:29:21

+0

謝謝你的答案,但我加了UserAccountEmail,我得到了接縫問題。 – CHANGYU 2015-02-25 03:32:50

回答

0

我解決了這個問題,

這是我的代碼:

public class BESGoogleContactsService 
{ 

    private const string serviceAccountEmail = "[email protected]"; 
    private const string serviceAccountCertPath = "BesSSO-123.p12"; 
    private const string serviceAccountCertPassword = "notasecret"; 
    private const string Email = "[email protected]"; 

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