如何检索Google通讯录?

时间:2018-02-23 05:59:38

标签: c# php google-api google-contacts google-people

我想让用户使用Google帐户登录并访问其联系人。我怎样才能实现它?我正在研究c#项目,但欢迎使用PHP解决方案。

1 个答案:

答案 0 :(得分:0)

我建议使用Google People API这是google contacts api的较新版本。

Oauth Link to code

private static UserCredential GetUserCredential(string clientSecretJson, string userName, string[] scopes)
    {
        try
        {
            if (string.IsNullOrEmpty(userName))
                throw new ArgumentNullException("userName");
            if (string.IsNullOrEmpty(clientSecretJson))
                throw new ArgumentNullException("clientSecretJson");
            if (!File.Exists(clientSecretJson))
                throw new Exception("clientSecretJson file does not exist.");

            // These are the scopes of permissions you need. It is best to request only what you need and not all of them               
            using (var stream = new FileStream(clientSecretJson, FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);

                // Requesting Authentication or loading previously stored authentication for userName
                var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
                                                                         scopes,
                                                                         userName,
                                                                         CancellationToken.None,
                                                                         new FileDataStore(credPath, true)).Result;

                credential.GetAccessTokenForRequestAsync();
                return credential;
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Get user credentials failed.", ex);
        }
    }

列表 Link to code

public static ListConnectionsResponse List(PeopleserviceService service, string resourceName, ConnectionsListOptionalParms optional = null)
    {
        try
        {
            // Initial validation.
            if (service == null)
                throw new ArgumentNullException("service");
            if (resourceName == null)
                throw new ArgumentNullException(resourceName);

            // Building the initial request.
            var request = service.Connections.List(resourceName);

            // Applying optional parameters to the request.                
            request = (ConnectionsResource.ListRequest)SampleHelpers.ApplyOptionalParms(request, optional);

            // Requesting data.
            return request.Execute();
        }
        catch (Exception ex)
        {
            throw new Exception("Request Connections.List failed.", ex);
        }
    }

    }

我的完整示例项目可以找到here