如何在ASP.NET MVC中导入Google联系人(不需要第三方) [英] How to import Google contacts in ASP.NET MVC (no third party please)

查看:366
本文介绍了如何在ASP.NET MVC中导入Google联系人(不需要第三方)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用API​​导入GMail的联系人。代码如下所示:

pre $ public $ data $ get $ $ $ $ $ {
Log.LogDebug(string.Format(SettingsController.cs-Importing Contacts for email = {0},password = {1} from gmail server,Uname,UPassword));
DataTable dt = new DataTable();
DataColumn C2 = new DataColumn();
C2.DataType = Type.GetType(System.String);
C2.ColumnName =EmailID;
尝试
{
dt.Columns.Add(C2);
RequestSettings rs =新的RequestSettings(App_Name,Uname,UPassword);
rs.AutoPaging = true;
ContactsRequest cr = new ContactsRequest(rs);
Feed<联系人> f = cr.GetContacts();
foreach(在f.Entries中联系t)
{
foreach(电子邮件在t.Emails中)
{
DataRow dr1 = dt.NewRow();
dr1 [EmailID] = email.Address.ToString();
dt.Rows.Add(dr1); Log.LogDebug(string.Format(SettingsController.cs-Imported Contacts for email = {0},password = {1} from gmail server,Uname,UPassword) );
return dt;
}
catch(Exception e)
{
dt = null;
Log.LogDebug(string.Format(SettingsController.cs-Imported Contacts for email = {0},password = {1} from gmail server,Uname,UPassword));
return dt;
}
}

此代码导入GMail联系人,但它是第三方DLL。所以有一段时间Google警告我不要使用它。所以我想使用Direct API。

解决方案


Google Contacts API的版本1和2具有已于2012年4月20日正式废止。它们将继续按照我们的弃用政策开展工作,但我们鼓励您迁移到版本3 - b $ b 发件人: < href =https://developers.google.com/google-apps/contacts/v2/developers_guide?hl=zh-CN =nofollow> https://developers.google.com/google-apps/contacts/ v2 / developers_guide?hl = zh_CN


使用OAuth 2.0授权请求



非Google公共用户数据的Google Contacts API请求必须由经过身份验证的用户授权。

OAuth 2.0的授权流程或流程的细节取决于您正在编写的应用程序类型。以下通用流程适用于所有应用程序类型:


  1. 当您创建应用程序时,您可以使用Google进行注册。
    Google随后会提供您稍后需要的信息,如客户
    ID和客户机密。 在$ Google
    API控制台。 (如果它未在控制台中列出,则跳过此
    步骤。)

  2. 当应用程序需要访问用户数据时,它会要求Google为
    a访问范围。

  3. Google向用户显示一个OAuth对话框,要求他们
    授权您的应用程序请求其某些数据。

  4. 如果用户批准,那么Google会为您的应用程序提供
    的短期访问令牌。

  5. 您的应用程序请求用户数据,将访问令牌附加到
    请求。

  6. 如果Google确定您的请求和令牌有效,那么
    会返回请求的数据。

发件人: https://developers.google.com/google-apps/contacts/v3/#authorizing_requests_with_oauth_20


请检查 Google Contacts API v2开发人员指南 - .Net Google Contacts API 3.0版来编写您自己的代码:)

I have Used a API for importing the contacts of GMail. The code is something like this:

public static DataTable GetGmailContacts(string App_Name, string Uname, string UPassword)
    {
        Log.LogDebug(string.Format("SettingsController.cs-Importing Contacts for email={0}, password={1} from gmail server", Uname, UPassword));
        DataTable dt = new DataTable();
        DataColumn C2 = new DataColumn();
        C2.DataType = Type.GetType("System.String");
        C2.ColumnName = "EmailID";
        try
        {
            dt.Columns.Add(C2);
            RequestSettings rs = new RequestSettings(App_Name, Uname, UPassword);
            rs.AutoPaging = true;
            ContactsRequest cr = new ContactsRequest(rs);
            Feed<Contact> f = cr.GetContacts();
            foreach (Contact t in f.Entries)
            {
                foreach (EMail email in t.Emails)
                {
                    DataRow dr1 = dt.NewRow();
                    dr1["EmailID"] = email.Address.ToString();
                    dt.Rows.Add(dr1);
                }
            }
            Log.LogDebug(string.Format("SettingsController.cs-Imported Contacts for email={0}, password={1} from gmail server", Uname, UPassword));
            return dt;
        }
        catch (Exception e)
        {
            dt = null;
            Log.LogDebug(string.Format("SettingsController.cs-Imported Contacts for email={0}, password={1} from gmail server", Uname, UPassword));
            return dt;
        }
    }

This code importing the GMail contacts but it is third party DLL. So some time Google warned me to not use it. So I want to use Direct API.

解决方案

Versions 1 and 2 of the Google Contacts API have been officially deprecated as of April 20, 2012. They will continue to work as per our deprecation policy, but we encourage you to move to version 3
From: https://developers.google.com/google-apps/contacts/v2/developers_guide?hl=en

Authorizing requests with OAuth 2.0

Requests to the Google Contacts API for non-public user data must be authorized by an authenticated user.

The details of the authorization process, or "flow," for OAuth 2.0 vary somewhat depending on what kind of application you're writing. The following general process applies to all application types:

  1. When you create your application, you register it with Google. Google then provides information you'll need later, such as a client ID and a client secret.
  2. Activate the Google Contacts API in the Services pane of the Google APIs Console. (If it isn't listed in the Console, then skip this step.)
  3. When your application needs access to user data, it asks Google for a particular scope of access.
  4. Google displays an OAuth dialog to the user, asking them to authorize your application to request some of their data.
  5. If the user approves, then Google gives your application a short-lived access token.
  6. Your application requests user data, attaching the access token to the request.
  7. If Google determines that your request and the token are valid, it returns the requested data.

From: https://developers.google.com/google-apps/contacts/v3/#authorizing_requests_with_oauth_20

Check Google Contacts API v2 Developer's Guide - .Net and Google Contacts API version 3.0 to write your own code :)

这篇关于如何在ASP.NET MVC中导入Google联系人(不需要第三方)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆