谷歌联系人API的OAuth2问题
问题描述:
我是谷歌应用程序的工作管理员,谷歌联系人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);
}
}
}
每个人都可以帮我解决这个问题呢?
答
我解决了这个问题,
这是我的代码:
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);
}
}
}
403是被禁止的错误,也有一些答案吧-http://*.com/questions/20372453/getting - 403,禁止为谷歌服务帐户 – yogihosting 2015-02-24 11:29:21
谢谢你的答案,但我加了UserAccountEmail,我得到了接缝问题。 – CHANGYU 2015-02-25 03:32:50