Как восстановить контакты Google?

Я хочу, чтобы пользователь вошел в систему с учетной записью Google и получил доступ к своим контактам. Как я могу достичь этого? Я работаю над проектом С#, но решение на PHP также приветствуется.


person Subham    schedule 23.02.2018    source источник


Ответы (1)


Я рекомендую использовать API Google People, это более новая версия API контактов Google.

Oauth Ссылка на код

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

Список Ссылка на код

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

    }

Мой полный образец проекта можно найти здесь

person DaImTo    schedule 23.02.2018