ErAmySharma / google-api-dotnet-client

Automatically exported from code.google.com/p/google-api-dotnet-client
Apache License 2.0
0 stars 0 forks source link

Code sample #506

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
using System; 
using System.Diagnostics; 
using System.Collections.Generic; 
using DotNetOpenAuth.OAuth2; 
using Google.Apis.Authentication.OAuth2; 
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth; 
using Google.Apis.Coordinate.v1; 
using Google.Apis.Coordinate.v1.Data;

namespace Google.Apis.Samples.CoordinateOAuth2
{ 
    /// <summary> 
    /// This sample demonstrates the simplest use case for an OAuth2 service. 
    /// The schema provided here can be applied to every request requiring authentication. 
    /// </summary> 
    public class ProgramWebServer
    { 
        public static void Main (string[] args)
        { 
            // TO UPDATE, can be found in the Coordinate application URL
            String TEAM_ID = "jskdQ--xKjFiFqLO-IpIlg"; 

            // Register the authenticator. 
            var provider = new WebServerClient (GoogleAuthenticationServer.Description);
            // TO UPDATE, can be found in the APIs Console.
            provider.ClientIdentifier = "335858260352.apps.googleusercontent.com";
            // TO UPDATE, can be found in the APIs Console.
            provider.ClientSecret = "yAMx-sR[truncated]fX9ghtPRI"; 
            var auth = new OAuth2Authenticator<WebServerClient> (provider, GetAuthorization); 

            // Create the service. 
            var service = new CoordinateService(new BaseClientService.Initializer()
                       {
                          Authenticator = auth
                       });

            //Create a Job Resource for optional parameters https://developers.google.com/coordinate/v1/jobs#resource 
            Job jobBody = new Job (); 
            jobBody.Kind = "Coordinate#job"; 
            jobBody.State = new JobState (); 
            jobBody.State.Kind = "coordinate#jobState"; 
            jobBody.State.Assignee = "user@example.com"; 

            //Create the Job 
            JobsResource.InsertRequest ins = service.Jobs.Insert (jobBody, TEAM_ID, "My Home", "51", "0", "Created this Job with the .Net Client Library");
            Job results = ins.Fetch (); 

            //Display the response 
            Console.WriteLine ("Job ID:"); 
            Console.WriteLine (results.Id.ToString ()); 
            Console.WriteLine ("Press any Key to Continue"); 
            Console.ReadKey (); 
        }

        private static IAuthorizationState GetAuthorization (WebServerClient client)
        { 
            IAuthorizationState state = new AuthorizationState (new[] { "https://www.googleapis.com/auth/coordinate" }); 
            // The refresh token has already been retrieved offline
            // In a real-world application, this has to be stored securely, since this token
            // gives access to all user data on the Coordinate scope, for the user who accepted the OAuth2 flow
            // TO UPDATE (see below the sample for instructions)
            state.RefreshToken = "1/0KuRg-fh9yO[truncated]yNVQcXcVYlfXg";

            return state;
        } 

    } 
}

 I need to implement the same mechanism with the new Google.Apis.Coordinate.v1 Client Library as I didn't want a redirection in my application.

Thanks,
Anish

Original issue reported on code.google.com by theanish...@gmail.com on 12 Nov 2014 at 8:25

GoogleCodeExporter commented 9 years ago
I'm still not sure what is missing to you right now (sorry).
Authenticate once (using the redirect page, authorize your app, and from that 
point the app will automatically refresh the token).

All documentation is available in: 
https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth

Sorry that I'm still not getting you - try to be more specific. Did you try to 
convert your code and follow the new OAuth 2.0 flow?

Original comment by pele...@google.com on 14 Nov 2014 at 5:29

GoogleCodeExporter commented 9 years ago
I am trying for the server side communication. So no redirection I want to 
perform(As it this is executed as part of the CRON scheduler job).
The refresh token has already been retrieved offline. The above code describes 
how to do the communication there after. But the code snippet uses old Google 
api library and is deprecated. I have downloaded the new version of the library 
from the Nugget. But the code snippet does not below,

 var provider = new WebServerClient(GoogleAuthenticationServer.Description);
                // TO UPDATE, can be found in the APIs Console.
                provider.ClientIdentifier = "335858260352.apps.googleusercontent.com";
                // TO UPDATE, can be found in the APIs Console.
                provider.ClientSecret = "yAMx-sR[truncated]fX9ghtPRI";
                var auth = new OAuth2Authenticator<WebServerClient>(provider, GetAuthorization);

                // Create the service. 
                var service = new CoordinateService(new BaseClientService.Initializer()
                {
                    Authenticator = auth
                });

does not have definition in the new library.

How can I achieve this with the new library.

Original comment by theanish...@gmail.com on 17 Nov 2014 at 5:51

GoogleCodeExporter commented 9 years ago
Take a look in our documentation in: 
https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth and 
especially in 
https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web_appl
ications.

We already have a sample for ASP.NET (not mvc) here:
https://code.google.com/p/google-api-dotnet-client/source/browse/?repo=samples#h
g%2FTasks.ASP.NET.SimpleOAuth2

We work hard on improving our documentation, so if something is missing, please 
let me know.
I keep this thread open, feel free to close it :) 

Original comment by pele...@google.com on 19 Nov 2014 at 2:35