My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GettingStarted  
Getting started using the library
Featured
Updated Dec 7, 2011 by asky...@google.com

In order to start developing .NET client applications which make use of the Google APIs, just follow this set of instructions.

1. Downloading the library

We recommend to start by downloading the zipped set of samples:

Or you can fetch them straight from our source control:

Once you have downloaded the file, unzip it.

2. Setting up the API Console

After you have downloaded the samples, you have to set up your API Consoles account before you can use them. The API Console can be found at:

GoogleAPI Console

2.1 Enabling APIs

Before you can run a sample using a specific API, you have to enable it in here and agree to its terms of service. We recommend experimenting with the "Tasks" API as a start.

2.2 Authenticating the Sample Project

Most of the APIs in the API Console require you to use OAuth2, or a developer key.

  • In the API Console select the API Access tab from the left hand menu
  • Click the big blue button "Create an OAuth 2.0 Client ID..."
  • On the first screen enter the product description you want your users to see when they grant you permission. This can just be "My Sample Application" for now. The logo field is optional.
  • On the second screen select "Installed application".
  • Click "Create Client ID".

Important! Try to keep your client secret and developer key secret! Anyone who has access to those can potentially abuse all the privileges granted to your application.

Your new Client ID and Client secret are now displayed (you will need these for the next step!). The API key is displayed in the Simple API Access section. If you have arrived at this screen, then go into the root Samples directory, and run the command below:

  • When you start the samples for the first time, they will prompt for you to enter the relevant credentials, which will be obfuscated and stored in your home folder.

3. A first example: Discovery.ListAPIs

As a first example you should have a look at the "Discovery.ListAPIs" directory. This sample makes use of the "Discovery API" to show a list of all API's with which this library can be used.

  • Open the solution file GoogleApisSamples.sln, or GoogleApisSamples_mono.sln if you are using MonoDevelop.
  • Right click on the project Discovery.ListAPIs and select "Set as StartUp Project".
  • Hit F5 to run this sample.

You should now see a list of APIs, comparable to the output below:

   [..]
   discovery:v1: APIs Discovery Service
   [..]

Congratulations! You have just made your first Google API call. When this ran it connected to our servers and got the latest list of services we make available. Now press any key to dismiss the console and open up Program.cs under Discovery.ListAPIs. This sample makes use of the "Discovery API" to show a list of all API's with which this library can be used. The source code for the sample is also visible below:

using System;
using Google.Apis;
using Google.Apis.Data;
using Google.Apis.Samples.Helper;

namespace Discovery.ListAPIs
{
    /// <summary>
    /// This example uses the discovery API to list all APIs in the discovery repository.
    /// http://code.google.com/apis/discovery/v1/using.html
    /// </summary>
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            // Display the header and initialize the sample.
            CommandLine.EnableExceptionHandling();
            CommandLine.DisplayGoogleSampleHeader("Discovery API");

            // Create the service.
            var service = new DiscoveryService();
            RunSample(service);
            CommandLine.PressAnyKeyToExit();
        }

        private static void RunSample(DiscoveryService service)
        {
            // Run the request.
            CommandLine.WriteAction("Executing List-request ...");
            var result = service.Apis.List().Fetch();

            // Display the results.
            if (result.Items != null)
            {
                foreach (DirectoryList.ItemsData api in result.Items)
                {
                    CommandLine.WriteResult(api.Id, api.Title);
                }
            }
        }
    }
}

What is important here?

  • In order to use a specific service, you have to add a reference to it. If you e.g. want to use the Tasks API, add the reference "Google.Apis.Tasks.V1.dll" to your project.
  • To create an instance of a service, just use "new ...Service();" (where ... is the name of the service, e.g. "Tasks" or "Discovery")
  • All methods of a service are located on individual resources on the services object. The Discovery-Service has an "Apis"-resource, which contains a "List" method.
  • As you call "service.Apis.List(..)", a Request-object targeting this method is returned. To execute a request, call the ".Fetch()" method on a request.
  • If a service requires a Developer Key, you can set the ".DeveloperKey"-property on the service object to the key you can find on the API Console.

4. Whats next?


Troubleshooting

"Access Not Configured" error

This exception occurs if you try to access an API which you did not enable in your API Console. Go to the link below, and enable access to the appropriate API:

API Console

After changing this setting it may take some minutes for the change to take effect.

System.Net.WebException when using the .NET Client Library under mono

Have a look at the MonoCompability page.

Comment by micro...@gmail.com, Feb 17, 2011

I can't compile this code, getting error on .ExecuteRequest?().

Comment by project member ccherub...@google.com, Feb 17, 2011

What is the error message?

Comment by ari...@gmail.com, Mar 7, 2011

.ExecuteRequest?() returns NullReferenceException?. stack trace is as follows

Google.Apis.Discovery.Parameter.get_Required() Google.Apis.MethodValidator?.ValidateParameter?(IParameter param) Google.Apis.MethodValidator?.ValidateAllParameters?() Google.Apis.Requests.Request.ExecuteRequest?() GoogleApiTest?.Program.Main(String args)
Comment by project member ccherub...@google.com, Mar 7, 2011

Are you using the latest version of the client library? That error was fixed in rev. 7412c9ee10

Comment by zhangcha...@gmail.com, Mar 16, 2011

the vs alert can't find using Google.Apis.Discovery; how can i you it?

Comment by test1.co...@gmail.com, Apr 14, 2011

Can you provide Sample code for URL shorterner?

Comment by tomas.ku...@gmail.com, May 25, 2011

I have problem with IService service = discovery.GetService?("v2", DiscoveryVersion?.Version_0_1, null); In discovery version is not Version_0_1 only 0_3, or 1_0. And as next, ThrowIfNull?("prarm") exception appear, when the program starts. Param is the last parameter in getService(). Thanks for help.

Comment by project member mlin...@google.com, May 25, 2011

I have updated the sample to work with the latest release of discovery (default branch). But I recommend switching over to the GoogleApis?.CodeGen-library - we will be publishing samples for it soon.

Comment by lxf...@yahoo.cn, May 30, 2011

Sorry.How can i get the reference these namespace :

using Google.Apis.Discovery; using Google.Apis.Json; using Google.Apis.Requests;

I mean how to get these dll.

Comment by project member ccherub...@google.com, May 30, 2011

The library is in alpha and dlls are not available for download yet.

You have to checkout the source as explained at http://code.google.com/p/google-api-dotnet-client/source/checkout and build the solution yourself.

Comment by lxf...@yahoo.cn, May 30, 2011

I check the url.I already creat a clone.I know that just means create a server-side clone. But what does the command line mean?how to execute it to get a local copy?I can not execute it at my local? Thank you!

Comment by project member ccherub...@google.com, May 30, 2011

You need to use a Mercurial client, I'd recommend checking the getting started guide at

http://mercurial.selenic.com/guide/

Comment by lxf...@yahoo.cn, May 30, 2011

Sorry.How to billing for this API? When I send a request to translate,it always give a message "day limit exceeded".So I think if I pay for the translation service ,the limitation will increase. If not,how can I increase my limitation? Thank you!

Comment by nemoryol...@gmail.com, Sep 16, 2011

VB Samples Please.

Comment by r.sa...@gmail.com, Oct 18, 2011

Here's a sample code in c# that i have tried out. It does a search on public activities.

         CommandLine.EnableExceptionHandling();            
         CommandLine.DisplayGoogleSampleHeader("PlusService API");     
         // Create the service.             
         var objService= new PlusService();
         objService.Key = ""; //put in uer key, not sure if it is mandatory

         var acts = objService.Activities.Search();
         acts.Query = "super cool";
         acts.MaxResults = 10;

         var searchResults = acts.Fetch();

         if (searchResults.Items != null)
         {
            foreach (Activity feed in searchResults.Items)
            {
               CommandLine.WriteResult(feed.Title, feed.Actor);//extract any property of uer interest
            }
         }

Hope this helps...

Comment by nathanco...@gmail.com, Nov 29, 2011

The enterCredentials.cmd file is referencing a path that is not included with the samples... EnterCredentials?\static\EnterCredentials?.exe

Powered by Google Project Hosting