2013-08-06 15 views
2

我正在尝试使用Oauth获取谷歌用户联系人。然而,代码示例和我的知识缺乏正在使这一难题成为现实。我使用这个页面作为参考。 https://developers.google.com/google-apps/contacts/v3/?csw=1#running_the_sample_code向Google联系人请求添加授权

这是我困惑的地方。

using Google.Contacts; 
using Google.GData.Contacts; 
using Google.GData.Client; 
using Google.GData.Extensions; 
    // ... 
    RequestSettings settings = new RequestSettings("<var>YOUR_APPLICATION_NAME</var>"); 
    // Add authorization token. 
    // ... 
    ContactsRequest cr = new ContactsRequest(settings); 
    // ... 

我不知道我需要什么,以填补在那里,或者如果代码是不够完整或如何向特定用户的联系人,我有一个很难理解的文档。充其量,我是个新手。任何帮助你可以提供将不胜感激。

回答

1

本示例将帮助您入门:

class OAuth2Demo { 
    private static string clientId; 
    private static string clientSecret; 
    private static string domain; 

    private static string applicationName = "Test-OAuth2"; 

    // Installed (non-web) application 
    private static string redirectUri = "urn:ietf:wg:oauth:2.0:oob"; 

    // Requesting access to Contacts API and Groups Provisioning API 
    private static string scopes = "https://www.google.com/m8/feeds/ https://apps-apis.google.com/a/feeds/groups/"; 

    /// <summary> 
    /// This console application demonstrates the usage of OAuth 2.0 with the Google Apps APIs. 
    /// </summary> 
    /// <param name="args">Command-line arguments: args[0] is 
    /// the client ID, args[1] is the client secret, args[2] is domain name. 
    /// </param> 
    public static void Main(string[] args) { 
     if (args.Length != 3) { 
      Console.WriteLine("Syntax: OAuth2Demo <client_id> <client_secret> <domain>"); 
     } else { 
      clientId = args[0]; 
      clientSecret = args[1]; 
      domain = args[2]; 

      OAuth2Parameters parameters = new OAuth2Parameters() { 
       ClientId = clientId, 
       ClientSecret = clientSecret, 
       RedirectUri = redirectUri, 
       Scope = scopes 
      }; 

      string url = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters); 
      Console.WriteLine("Authorize URI: " + url); 
      parameters.AccessCode = Console.ReadLine(); 

      OAuthUtil.GetAccessToken(parameters); 

      // Testing OAuth 2.0 with a Request-based library 
      RunContactsSample(parameters); 

      // Testing OAuth 2.0 with a Service-based library 
      RunGroupsSample(parameters, domain); 
     } 
    } 

    /// <summary> 
    /// Send authorized queries to a Request-based library 
    /// </summary> 
    /// <param name="service"></param> 
    private static void RunContactsSample(OAuth2Parameters parameters) { 
     try { 
      RequestSettings settings = new RequestSettings(applicationName, parameters); 
      ContactsRequest cr = new ContactsRequest(settings); 

      Feed<Contact> f = cr.GetContacts(); 
      foreach (Contact c in f.Entries) { 
       Console.WriteLine(c.Name.FullName); 
      } 
     } catch (AppsException a) { 
      Console.WriteLine("A Google Apps error occurred."); 
      Console.WriteLine(); 
      Console.WriteLine("Error code: {0}", a.ErrorCode); 
      Console.WriteLine("Invalid input: {0}", a.InvalidInput); 
      Console.WriteLine("Reason: {0}", a.Reason); 
     } 
    } 

    /// <summary> 
    /// Send authorized queries to a Service-based library 
    /// </summary> 
    /// <param name="service"></param> 
    private static void RunGroupsSample(OAuth2Parameters parameters, string domain) { 
     try { 
      GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory("apps", applicationName, parameters); 

      GroupsService service = new GroupsService(domain, applicationName); 
      service.RequestFactory = requestFactory; 

      GroupFeed feed = service.RetrieveAllGroups(); 
      foreach (GroupEntry group in feed.Entries) { 
       Console.WriteLine(group.GroupName); 
      } 
     } catch (AppsException a) { 
      Console.WriteLine("A Google Apps error occurred."); 
      Console.WriteLine(); 
      Console.WriteLine("Error code: {0}", a.ErrorCode); 
      Console.WriteLine("Invalid input: {0}", a.InvalidInput); 
      Console.WriteLine("Reason: {0}", a.Reason); 
     } 
    } 
} 

我是从here。 与您的应用程序一切顺利。