3

I have a method to create folder on Google drive as soon a create method is hit. I want the folder to created once the batch is saved. Below is the code Im using. In the below method page redirects as soon as it is authorised. So requesting for workarround

 def create
    client = Signet::OAuth2::Client.new(
      :authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
      :token_credential_uri =>  'https://www.googleapis.com/oauth2/v3/token',
      :client_id => "XXXXXX-XXXXXX",
      :client_secret => "XXXXXX-XXX",
      :scope => 'https://www.googleapis.com/auth/drive',
      :redirect_uri => "http://localhost:3000/batches/"
      )
    @batch = Batch.new(params[:batch])

    respond_to do |format|
      if @batch.save

        if params[:code].present?
          client.code = request['code']
          client.fetch_access_token!
          x = Google::Apis::DriveV3::DriveService.new
          x.authorization = client
          files = x.list_files(options: { authorization: client })
          file_metadata = {
            name: 'Batches-new',
            mime_type: 'application/vnd.google-apps.folder',
          }
        file = x.create_file(file_metadata, fields: 'id')
            Rails.logger.debug "Folder Id: #{file.id}"
        else
          format.html {redirect_to client.authorization_uri.to_s}
        end

        format.html {redirect_to batches_path(trek_id: @batch.trek_id), notice: 'Batch was successfully created.' }
        format.json { render json: @batch, status: :created, location: @batch }
      else
        format.html {
          flash.now[:error] = @batch.errors.full_messages
          render action: "new"
        }
        format.json { render json: @batch.errors, status: :unprocessable_entity }
      end
    end
  end

after hiting the create action page navigates to index page without creating folder. How to make folder gets created before going to index page. Thanx in advance.

ChandraKanth17
  • 91
  • 2
  • 10
  • I don't like this work flow at all. how are you going to handle refresh tokens? you are mixing authentication with actions in your controller. i would keep them entirely separate: get authenticated first before doing anything is probably how i would do it. – BenKoshy May 04 '17 at 13:13
  • @BKSpurgeon. I tried taking out the authentication to many different methods. and moreover, I don't need this authentication as primary. I was using google drive to just create a folder. So after looking out many steps I got this and used. If you know any better I can do it. I will be very thankful. All I need to do is just after the create post method a folder with that batch name needs to be created. – ChandraKanth17 May 04 '17 at 16:35
  • looks promising: https://richonrails.com/articles/google-authentication-in-ruby-on-rails/ – BenKoshy May 04 '17 at 22:49
  • https://github.com/zquestz/omniauth-google-oauth2 that should give you some ideas – BenKoshy May 04 '17 at 22:49
  • Hi the above document is about using oauth as main login system. But all I need to do is create a folder in my drive account and I don't want everytime user to log in and log out. The folder should be created as soon a new batch is created.Its just one account where the folder is created and shared across all users. So is there any simple mechanism to use on drive folder creation than this? It is fine even I want to use login and logout details in code. - @BKSpurgeon – ChandraKanth17 May 05 '17 at 05:47
  • store the authenticity and refresh tokens so that you don't have to manually log in each time. you will only have to do it the first time. or you could possibly use a service account: but i can't advise you any more beyond that. i hope it helps you: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority – BenKoshy May 05 '17 at 06:34
  • @BKSpurgeon Thank you very much for the support. – ChandraKanth17 May 05 '17 at 11:57
  • please do post your solution when you find it - it may probably help someone else in a similar situation in the future. – BenKoshy May 05 '17 at 14:19
  • Yeah i will surely do that im still working over it @BKSpurgeon – ChandraKanth17 May 05 '17 at 15:07

0 Answers0