kelvinRosa / 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

Get error 400 when authorizing. #357

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Trying to list all users of domain using following code:

----------------------------------------------------------
using System;
using System.Diagnostics;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

using DotNetOpenAuth.OAuth2;

using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Samples.Helper;
using Google.Apis.Services;
using Google.Apis.Util;
using Google.Apis.Admin.directory_v1;
using Google.Apis.Admin.directory_v1.Data;

namespace Bergstedts.ListUsers
{
    public class Program
    {
        static void Main(string[] args)
        {
             // Display the header and initialize the sample.
            CommandLine.EnableExceptionHandling();
            CommandLine.DisplayGoogleSampleHeader("Lists all Users");

            // Register the authenticator.
            Console.WriteLine("Register authenticator");
            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description)
                {
                    ClientIdentifier = "<myClientIdentifier>",
                    ClientSecret = "<myClientSecret>"                   
                };

            var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);

            // Create the service.
            Console.WriteLine("Create service");
            var service = new DirectoryService(new BaseClientService.Initializer()
                {
                    Authenticator = auth,
                    ApplicationName = "List Users",
                    GZipEnabled = true,
                    ApiKey = "<myApiKey>"
                });

            service.Users.List().Domain = "<myDomain.com>";
            Console.WriteLine("Get list");
            Users results = service.Users.List().Execute();

            Console.WriteLine("Users:");
            foreach (User list in results.UsersValue)
            {
                Console.WriteLine("- " + list.Name);
            }
            Console.ReadKey();
        }

        private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            // Get the auth URL:
            IAuthorizationState state = new AuthorizationState(new[] { DirectoryService.Scopes.AdminDirectoryUser.GetStringValue() });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            Uri authUri = arg.RequestUserAuthorization(state);

            // Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString());
            Console.Write("  Authorization Code: ");
            string authCode = Console.ReadLine();
            Console.WriteLine();

            // Retrieve the access token by using the authorization code:
            Console.WriteLine("Retrieving the authorization code");
            return arg.ProcessUserAuthorization(authCode, state);
            Console.WriteLine("Retrieved the authorization code");
        }

    }
}
---------------------------------------------------------------
I get the following error :
---------------------------------------------------------------
 An error has occured:
    Google.Apis.Requests.RequestError
Bad Request [400]
Errors [
        Message[Bad Request] Location[ - ] Reason[badRequest] Domain[global]
]
-----------------------------------------------------------------

Where do I go wrong???

Original issue reported on code.google.com by jo...@bergstedts.mygbiz.com on 20 Jul 2013 at 4:36

GoogleCodeExporter commented 9 years ago
In which line the exception is?
Can you print the stacktrace?

Original comment by pele...@google.com on 23 Jul 2013 at 9:20

GoogleCodeExporter commented 9 years ago
We are changing the whole model of OAuth2 in the next version.
I'm closing this issue cause I didn't get a response back in the last 2 months.
Feel free to reopen :)

Original comment by pele...@google.com on 27 Sep 2013 at 10:02