1

I keep getting Authorization errors when sending a POST request from my React/Ruby app. I'm getting different errors depending on what OOB_URI I'm using.

If I use OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze then I get the following error: enter image description here

When I use OOB_URI = "http://127.0.0.1".freeze then I get the following error: enter image description here

Here's my Ruby code

require "google/apis/sheets_v4"
require "googleauth"
require "googleauth/stores/file_token_store"
require "fileutils"

# Get authorization
OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
# OOB_URI = "http://127.0.0.1".freeze
APPLICATION_NAME = "mimirgettingmarried".freeze
CREDENTIALS_PATH = "/Users/michelleroos/Desktop/mimirgettingmarried/mimirgettingmarried/config/credentials.json".freeze
# The file token.yaml stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
TOKEN_PATH = "token.yaml".freeze
SCOPE = Google::Apis::SheetsV4::AUTH_SPREADSHEETS

# Ensure valid credentials, either by restoring from the saved credentials
# files or intitiating an OAuth2 authorization. If authorization is required,
# the user's default browser will be launched to approve the request.

# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
def authorize
  client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH
  token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH
  authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store
  user_id = "default"
  credentials = authorizer.get_credentials user_id
  if credentials.nil?
    url = authorizer.get_authorization_url base_url: OOB_URI
    puts "Open the following URL in the browser and enter the " \
         "resulting code after authorization:\n" + url
    code = gets
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI
    )
  end
  credentials
end

# Initialize the API
service = Google::Apis::SheetsV4::SheetsService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize

spreadsheet_id = '17bXAJELWjXIRmcJ-RPeg5_W4wszqZI3QxvE_ZIL3L6A'
range = 'rsvp'

class Api::RsvpsController < ApplicationController

  def create # append

    values = [
      'hi', 'hello'
    ]

    # TODO: Assign values to desired members of `request_body`:
    request_body = Google::Apis::SheetsV4::ValueRange.new(values: values)

    response = service.append_spreadsheet_value(spreadsheet_id, range, request_body)

    # TODO: Change code below to process the `response` object:
    puts response.to_json

  end

  def show

    # range = "Class Data!A2:E"
    # response = service.get_spreadsheet_values(spreadsheet_id, range)
    # puts "Name, Major:"
    # puts "No data found." if response.values.empty?
    # response.values.each do |row|
    #   # Print columns A and E, which correspond to indices 0 and 4.
    #   puts "#{row[0]}, #{row[4]}"
    # end

    range_names = [
      # Range names ...
    ]
    result = service.batch_get_spreadsheet_values(spreadsheet_id)
    # result = service.batch_get_spreadsheet_values(spreadsheet_id, ranges: range_names)
    puts "#{result.value_ranges.length} ranges retrieved."
  end

end

Here's how I set up my credentials in Google Cloud Console: enter image description here

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
helloroos
  • 293
  • 1
  • 2
  • 11
  • If this is a desktop app why did you create Web application credentials? Try adding redirect Uri of http ://127.0.0.1/oauth2callback If you are trying to create a web application then you should be following [web](https://github.com/googleapis/google-api-ruby-client/tree/main/samples/web) – Linda Lawton - DaImTo Apr 29 '22 at 07:13
  • Let me confirm that I understand the difference correctly. "A Desktop App is a computer program that runs locally on a computer device like a desktop or a laptop whereas a Web App needs an internet connection or some sort of network to work properly." It's a wedding website, intended to work on desktop and mobile devices using internet connection. – helloroos Apr 29 '22 at 07:41
  • A web app is hosted on a webserver. A desktop application is installed on a computer for example Google chrome is installed on your computer. A mobile application is an application run on a mobile device. Each of these application types have their own credential types, the code to use those credential types is different. You are using the code designed for an installed application with the credentials designed for use with a web application. This wont work pick one. – Linda Lawton - DaImTo Apr 29 '22 at 08:17
  • If you are making a webesite then follow this example instead [web](https://github.com/googleapis/google-api-ruby-client/tree/main/samples/web) You just need to swap out the calendar / drive code with the sheets Code you have. Its really just the authorization code you need to change everything else should be the same. – Linda Lawton - DaImTo Apr 29 '22 at 08:21

0 Answers0