Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

access_token on Bitrix instance not working (always 401) #55

Open
mruniverse opened this issue Sep 20, 2023 · 11 comments
Open

access_token on Bitrix instance not working (always 401) #55

mruniverse opened this issue Sep 20, 2023 · 11 comments
Assignees

Comments

@mruniverse
Copy link

I've already tested in other versions and it works fine.

@mrhyde
Copy link
Member

mrhyde commented Sep 20, 2023

Hi! Do you mean other versions of this library or some other packages?
Can you provide an example that leads to an error? Be careful not to expose you access_token

@mruniverse
Copy link
Author

Other versions of this library. Sure! i'm going to prepare an example in just a moment.

@foresh9
Copy link

foresh9 commented Oct 27, 2023

I have the same problem.
Always returns Response code 401 (Unauthorized)
The token is definitely valid, because if you use the Bitrix API, there is no such error.

  import { Bitrix } from '@2bad/bitrix'

  const access_token = "7fe33a650068085800680852000000010000";
  const bitrix_service = Bitrix('https://b24-6.bitrix24.ru/rest', access_token)
   
bitrix_service.deals
    .get("1")
    .then(({ result }) => {
const { TITLE } = result; 
      console.log(TITLE);
    })
    .catch(console.error);

@friench
Copy link

friench commented Jul 16, 2024

I have the same problem when creating a Bitrix application and then authenticate with an OAuth=(

https://stackblitz.com/edit/2bad-bitrix-401-error?file=index.js

Screenshot 2024-07-16 at 23 44 41

Everything works in Postman:
Screenshot 2024-07-16 at 23 45 49

@mruniverse @foresh9 did you manage to solve the problem?

@friench
Copy link

friench commented Jul 17, 2024

Issue: The token addition hook uses the deprecated options.path property.

Problematic file: addAccessToken.ts

Issue reference: Got issue #955

Here's my version of the file where I replaced options.path with options.url.search:

import { BeforeRequestHook } from 'got'

export default (accessToken?: string): BeforeRequestHook => (options) => {
  if (!accessToken) return
  options.url.search = `${options.url.search}${options.url.search ? '&' : '?'}access_token=${accessToken}`
}

@mrhyde Could you commit this fix to the repository?

@mrhyde mrhyde self-assigned this Jul 17, 2024
@mrhyde
Copy link
Member

mrhyde commented Jul 17, 2024

@friench I'll check it out tomorrow and try to get the fix published ASAP.

@mrhyde
Copy link
Member

mrhyde commented Jul 18, 2024

@friench In the meantime, could you share how you generated the API token? With all the Bitrix updates over the past few years, I can't seem to find the exact page anymore. Much appreciated!

@mrhyde
Copy link
Member

mrhyde commented Jul 18, 2024

The fix has been published to the beta branch (3.0.0-beta.0). You can try it by running npm install @2bad/bitrix@beta.
Please note that the package is now ESM-only.

@friench
Copy link

friench commented Jul 19, 2024

@friench In the meantime, could you share how you generated the API token? With all the Bitrix updates over the past few years, I can't seem to find the exact page anymore. Much appreciated!

When the Bitrix24 application communicates with my backend, it sends objects in the body and query of the POST request in the following format:

Body:

{
  "AUTH_ID": "ee659a66006fa7a40560d61da6545d15a60a1c6f",
  "AUTH_EXPIRES": "3600",
  "REFRESH_ID": "dee4c166006fa7a4006fa7ac833c1db6411d8fdb8141dd",
  "member_id": "5d08a1d6854bf6fa66f498c",
  "status": "L",
  "PLACEMENT": "DEFAULT",
  "PLACEMENT_OPTIONS": "{\"install_finished\":\"Y\",\"any\":\"2\\/\"}"
}

Query:

{
  "DOMAIN": "b24-jjv8xi.bitrix24.ru",
  "PROTOCOL": "1",
  "LANG": "ru",
  "APP_SID": "72a874549a9ba00c8ec26235882aee9a"
}

Explanation of Key Parameters:
AUTH_ID: Authorization token
REFRESH_ID: Refresh token
DOMAIN: Portal domain

These parameters are then used to initialize the client constructor. For more detailed information, refer to the official documentation here.

@mrhyde
Copy link
Member

mrhyde commented Jul 19, 2024

Thanks. Can you confirm that beta version working okay for you?

@friench
Copy link

friench commented Jul 31, 2024

@mrhyde Yes, the new version works correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants