0

Google Contacts API を使用してサービス アカウントから連絡先情報を取得できませんでした (ライブ ユーザー セッションを使用していません)。何を試しても、例外が発生するか (たとえば、Message = "Error:\"invalid_request\", Description:\"Invalid impersonation prn email address.\", Uri:\"\"")、または例外が発生します。エラーが返されましたが、0 件の連絡先が返されました。

Google Apps 管理コンソールと Manage API Client Access ページの設定を再確認しましたが (Google App テクニカル サポートからこれらの設定を確認することも含めて)、連絡先を読み込むことができません。また、「Stack Overflow」で見つけたすべての例を試しましたが、どれも問題を解決していないようです。

これらの試みはC#/ASP.netで行われましたが、私は他の言語に精通しているので、誰かが持っている可能性のある例を適応させることができるはずです。

誰かがサービス アカウントから Google Contacts API の使用に成功し、それを達成するために行ったコードを喜んで共有してくれることを願っています。

ありがとうございました!!!

以下は、常に例外 "invalid_request\": "Invalid impersonation prn email address.\" が発生する試みの例です。

    private void TestLoadContacts()
    {
        try
        {
            string strClientID = "STRINGGENERATEDFROMGOOGLEAPPSINTERFACE.apps.googleusercontent.com";
            var credential = GenerateCred(new[] { "https://www.google.com/m8/feeds" }, strClientID);

            // Get the token for this scope and user
            if (credential.RequestAccessTokenAsync(new CancellationToken()).Result)
            {
                // use the token to initalize the request
                var rs = new RequestSettings("Google Sync")
                {
                    OAuth2Parameters = new OAuth2Parameters()
                    {
                        AccessToken = credential.Token.AccessToken
                    }
                };

                var request = new ContactsRequest(rs);
                Feed<Contact> f = request.GetContacts();
                foreach (var c in f.Entries)
                {
                    //process each contact
                }
            }
        }
        catch (Exception ex)
        {
            string strError = ex.Message;
        }
    }

    private static ServiceAccountCredential GenerateCred(IEnumerable<string> scopes, string delegationUser)
    {
        string strServiceAccountEmail = "account-1@MyGoogleAccount-1139.iam.gserviceaccount.com";
        X509Certificate2 certificate = new X509Certificate2(@"C:\MyP12s\MyGoogleAccount-9da1a08f4eef.p12",
            "notasecret", X509KeyStorageFlags.Exportable);

        var credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(strServiceAccountEmail)
            {
                Scopes = scopes,
                User = delegationUser
            }.FromCertificate(certificate));
        return credential;
    }
4

1 に答える 1