0

I am working with the Google Provisioning API. I have used Web Application type project from Google developer console. I have used Diamto blog and samples and it works perfectly on my local with all options like FileStore, Custom File Store, Service Account etc but when I uploaded on server user consent screen just doesn't pops up with any options like FileStore, Custom File Store. I spent days to figure out problem and solutions but nothing has worked for me so far.

my configuration

  1. My server configuration is windows server 2008 datacenter r2,.net 4.5,IIS 7.5.
  2. Service account works perfectly but I need to do it by Consent screen so Web Application type of project.
  3. I have used google .net client library with version 1.9.2.27817.
  4. I am just highlighting main code where it gets stuck and rest is same as per Diamto post and github examples.

Let me know if you need more info.

Code

public static DirectoryService AuthenticateOauth(string clientId, string clientSecret, string userName, IDataStore datastore)
{
    string[] scopes = new string[] {DirectoryService.Scope.AdminDirectoryUser };    

            try
            {
                // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                             , scopes
                                                                                             , userName
                                                                                             , CancellationToken.None
                                                                                             , datastore).Result; // at this point it calls getasynch method for custom datasource

                DirectoryService service = new DirectoryService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "GoogleProv",
                });
                return service;
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.InnerException);
                return null;

            }

        }
                {
                    HttpClientInitializer = credential,
                    ApplicationName = "GoogleProv",
                });
                return service;
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.InnerException);
                return null;

            }

        }


  ///<summary>
        // Returns the stored value for the given key or <c>null</c> if the matching file (<see cref="GenerateStoredKey"/>
        // in <see cref="FolderPath"/> doesn't exist.
        // </summary>
        // <typeparam name="T">The type to retrieve</typeparam>
        // <param name="key">The key to retrieve from the data store</param>
        // <returns>The stored object</returns>




        public Task<T> GetAsync<T>(string key)
        {
            //Key is the user string sent with AuthorizeAsync
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("Key MUST have a value");
            }
            TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();


            // Note: create a method for opening the connection.
            SqlConnection myConnection = new SqlConnection(myconn);
            myConnection.Open();

            // Try and find the Row in the DB.
            using (SqlCommand command = new SqlCommand("select RefreshToken from GoogleUser where UserName = @username;", myConnection))
            {
                command.Parameters.AddWithValue("@username", key);

                string RefreshToken = null;
                SqlDataReader myReader = command.ExecuteReader();
                while (myReader.Read())
                {
                    RefreshToken = myReader["RefreshToken"].ToString();
                }

                if (RefreshToken == null )
                {
                    // we don't have a record so we request it of the user.
                    tcs.SetResult(default(T)); // it comes here
                }
                else
                {

                    try
                    {
                        // we have it we use that.
                        tcs.SetResult(NewtonsoftJsonSerializer.Instance.Deserialize<T>(RefreshToken));
                    }
                    catch (Exception ex)
                    {
                        tcs.SetException(ex);
                    }

                }
            }

            return tcs.Task; // it comes here and than gets hang forever
        }

Any of your help is highly appreciated.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • did you add the redirect uri in the google Developer console to your production server – Linda Lawton - DaImTo Aug 12 '15 at 14:34
  • yes its there.it gets stuck at last statement of public Task GetAsync(string key) function which is return tcs.Task and give an exception "Thread was being aborted". sorry for comming late because I didn't get any comment or help so left it but I am still looking for a solution. Thanks. – user3502954 Sep 28 '15 at 13:52
  • Redirect URI is set in the Google Developer console not in your code. The client library sends the redirect uri of where it is coming from – Linda Lawton - DaImTo Sep 28 '15 at 13:54
  • yes I set in google developer console and it works perfect on my local.I have same code as of your website just connection string and account credentials have been changed. As per my question all those scenarios works perfectly on server too along with using http://stackoverflow.com/questions/27573272/googlewebauthorizationbroker-authorizeasync-hangs approach but I would like to use above approach so that I can have logic in my DAL layer only. Thanks. – user3502954 Sep 29 '15 at 12:30
  • if AuthorizeAsync Hangs go to the previous version of the nugget package there is an issue with the current version. – Linda Lawton - DaImTo Sep 29 '15 at 12:32
  • I have used Google.Apis.Auth.1.9.2\lib\net40\Google.Apis.Auth.PlatformServices.dll version. do you want me to use previous version than this ? – user3502954 Sep 29 '15 at 13:44
  • you didn't use a nugget package? – Linda Lawton - DaImTo Sep 29 '15 at 13:49
  • I tried to use earlier version of package 1.8 but it gave me below error so I cant use that package. Error = Method not found: 'Void Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow..ctor(Initializer)'. – user3502954 Sep 30 '15 at 10:35
  • is this a cloud server or your own server? – Linda Lawton - DaImTo Sep 30 '15 at 10:38

0 Answers0