react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

Getting Unauthorized access (Error code 401)

Open GurjeetLamba opened this issue 2 years ago • 13 comments

Your Environment

Plugin version:4.12.1 Platform: Android OS version: Device manufacturer / model: OPPO/CPH2095 React Native version (react-native -v): 0.64

  • Plugin config

const state:State = await BackgroundGeolocation.ready({

   url:"http://92.204.135.120:8099/TomAPI/SaveLocationInformation",
  
  locationTemplate: '{"Latitude":<%= latitude %>,"Longitude":<%= longitude %>,"TimeStamp": "<%= timestamp %>"}',
  autoSync:true,
  autoSyncThreshold:5,
  batchSync:true,
  maxBatchSize:50,
  params: {
  ClientId: 69,
  },
  headers: {
 
 "authorization":'Bearer '  + getToken.data.Token ,
    "X-FOO": "BAR"
  },
  logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
 //transistorAuthorizationToken: token,
  // Geolocation
  desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_NAVIGATION,
  distanceFilter: 10,
  stopTimeout: 5,
  // Permissions
  locationAuthorizationRequest: 'Always',
  backgroundPermissionRationale: {
    title: "Allow {applicationName} to access this device's location even when closed or not in use.",
    message: "This app collects location data to enable recording your trips to work and calculate distance-travelled.",
    positiveAction: 'Change to "{backgroundPermissionOptionLabel}"',
    negativeAction: 'Cancel'
  },
  // HTTP & Persistence
locationsOrderDirection:"DESC",
  maxDaysToPersist: 14,
  // Application
  stopOnTerminate: false,
  startOnBoot: true,
  enableHeadless: true
});

GurjeetLamba avatar Jun 28 '23 12:06 GurjeetLamba

401 comes from your server.

Why do you leave the sample header "X-FOO": "BAR" in your headers?

christocracy avatar Jun 28 '23 13:06 christocracy

I have Tried removing it Also , but otherwise my syntax for writing token is correct?

GurjeetLamba avatar Jun 28 '23 13:06 GurjeetLamba

Why don’t you debug this at your server and observe the incoming headers to determine why your server does not like the provided token?

christocracy avatar Jun 28 '23 13:06 christocracy

That I will do after making sure I am sending the token in correct way from my app, I have just followed the documentation for writing url and token

GurjeetLamba avatar Jun 28 '23 13:06 GurjeetLamba

See Mozilla MDN docs for information about Bearer token.

christocracy avatar Jun 28 '23 13:06 christocracy

I Use the Bearer token while calling other API's so nothing wrong in that , But should I use the authorization keyword only ?

GurjeetLamba avatar Jun 28 '23 13:06 GurjeetLamba

headers: {

"authorization":'Bearer ' + getToken.data.Token ,

  },

GurjeetLamba avatar Jun 28 '23 13:06 GurjeetLamba

The Config.headers are applied to the http request exactly as provided. There is nothing unusual about your authorization header.

consult with your server logs.

christocracy avatar Jun 28 '23 13:06 christocracy

Hi I pass a source parameter also inside my header when I call any API ,May be I am getting error due to this , What will be the way to pass source inside header with double quotes or without double quotes ?

Screenshot 2023-06-29 at 3 46 13 PM

GurjeetLamba avatar Jun 29 '23 10:06 GurjeetLamba

HTTP header values are always of type String

christocracy avatar Jun 29 '23 11:06 christocracy

so I should use { "source": "92.204.135.120" }

?

GurjeetLamba avatar Jun 29 '23 12:06 GurjeetLamba

“HTTP header values are always of type Stringimplies that values must always be enclosed in quotes.

christocracy avatar Jun 29 '23 12:06 christocracy

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar May 20 '24 01:05 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jun 03 '24 01:06 github-actions[bot]