【问题标题】:How to retrieve Google Contacts?如何检索谷歌通讯录?
【发布时间】:2018-02-23 05:59:38
【问题描述】:

我想让用户使用 google 帐户登录并访问他们的联系人。我怎样才能实现它?我正在开发 c# 项目,但也欢迎使用 PHP 解决方案。

【问题讨论】:

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


    【解决方案1】:

    我建议使用Google People API,它是谷歌通讯录 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

    【讨论】:

      猜你喜欢
      • 2014-12-02
      • 1970-01-01
      • 2015-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多