|
| 1 | +class CallbacksController < ApplicationController |
| 2 | + |
| 3 | + def index |
| 4 | + #Token.delete_all |
| 5 | + tkn = Token.first |
| 6 | + |
| 7 | + if tkn.nil? |
| 8 | + redirect_to "/auth/google" and return |
| 9 | + else |
| 10 | + if tkn['spreadsheet_id'].nil? |
| 11 | + @ws = getSheet(tkn) |
| 12 | + render action: 'create' and return |
| 13 | + end |
| 14 | + end |
| 15 | + @sheets = Token.all.to_a |
| 16 | + end |
| 17 | + |
| 18 | + def omniauth |
| 19 | + tkn = Token.new |
| 20 | + req = request.env["omniauth.auth"] |
| 21 | + auth_token = request.env["omniauth.auth"].fetch("credentials") |
| 22 | + tkn["access_token"] = auth_token.fetch("token") |
| 23 | + tkn["refresh_token"] = auth_token.fetch("refresh_token") |
| 24 | + tkn["expires_at"] = auth_token.fetch("expires_at") |
| 25 | + |
| 26 | + if tkn.save |
| 27 | + @ws = getSheet(tkn) |
| 28 | + else |
| 29 | + @ws = {error: "not able to save"} |
| 30 | + end |
| 31 | + |
| 32 | + render action: 'create' and return |
| 33 | + end |
| 34 | + |
| 35 | + def getSheet(tkn) |
| 36 | + client = Google::APIClient.new |
| 37 | + client.authorization.access_token = tkn['access_token'] |
| 38 | + #session = GoogleDrive.login_with_oauth(tkn["access_token"]) |
| 39 | + drive = client.discovered_api('drive', 'v3') |
| 40 | + |
| 41 | + wks = client.execute( |
| 42 | + api_method: drive.files.list, |
| 43 | + parameters: {}, |
| 44 | + headers: {'Content-Type'=>'application/json'} |
| 45 | + ) |
| 46 | + JSON.parse(wks.data.to_json) |
| 47 | + end |
| 48 | + |
| 49 | + def setSheet |
| 50 | + tkn = Token.first |
| 51 | + tkn['spreadsheet_id'] = params['id'] |
| 52 | + tkn.save |
| 53 | + |
| 54 | + insertEmailInDatabase(tkn) |
| 55 | + |
| 56 | + redirect_to users_path |
| 57 | + end |
| 58 | + |
| 59 | + def insertEmailInDatabase(tkn) |
| 60 | + @tkn = tkn |
| 61 | + session = GoogleDrive.login_with_oauth(@tkn["access_token"]) |
| 62 | + @ws = session.spreadsheet_by_key(@tkn['spreadsheet_id']).worksheets[0] |
| 63 | + |
| 64 | + rowcount = @ws.rows.count |
| 65 | + usercount = User.count |
| 66 | + #if rowcount > usercount |
| 67 | + (rowcount).times do |i| |
| 68 | + User.create(email_id: @ws[i + 1, 1], # + usercount, 1], |
| 69 | + is_subscribed: true, |
| 70 | + joined_on: Date.today, |
| 71 | + source: "Google Spreadsheet") |
| 72 | + end |
| 73 | + #end |
| 74 | + |
| 75 | + #render action: "test" |
| 76 | + end |
| 77 | + |
| 78 | + def refresh_token(tkn) |
| 79 | + data = { |
| 80 | + client_id: ENV['GOOGLE_ID'], |
| 81 | + client_secret: ENV['GOOGLE_KEY'], |
| 82 | + refresh_token: tkn['refresh_token'], |
| 83 | + grant_type: 'refresh_token' |
| 84 | + } |
| 85 | + re = ActiveSupport::JSON.decode(RestClient.post 'https://accounts.google.com/o/oauth2/token', data) |
| 86 | +=begin |
| 87 | + client = Google::APIClient.new |
| 88 | + client.authorization.access_token = tkn['access_token'] |
| 89 | + client.authorization.client_id = ENV["GOOGLE_ID"] |
| 90 | + client.authorization.client_secret = ENV["GOOGLE_KEY"] |
| 91 | + client.authorization.refresh_token = tkn["refresh_token"] |
| 92 | + re = client.authorization.update_token! |
| 93 | +=end |
| 94 | + sheets = Token.where(spreadsheet_id: tkn['spreadsheet_id'])[0] |
| 95 | + sheets['access_token'] = re['access_token'] |
| 96 | + sheets['expires_at'] = (Time.now + re['expires_in'].second).localtime |
| 97 | + sheets.save |
| 98 | + |
| 99 | + re |
| 100 | + end |
| 101 | + |
| 102 | + def update |
| 103 | + tkn = Token.where(spreadsheet_id: params['id']) |
| 104 | + @tkn = tkn[0] |
| 105 | + unless Time.now > tkn[0]['expires_at'] |
| 106 | + @ref = refresh_token tkn[0] |
| 107 | + end |
| 108 | + tkn = Token.where(spreadsheet_id: params['id']) |
| 109 | + insertEmailInDatabase(tkn[0]) |
| 110 | + redirect_to users_path |
| 111 | + #render action: 'test' |
| 112 | + end |
| 113 | + |
| 114 | +end |
0 commit comments