Why is the route mypreferences/locale not working?

Hello,

I’m developing an app for Jira and I’m using the Jira REST API (in React and Forge), and the route GET /rest/api/3/mypreferences/locale does not work on my application, and I need it to get the user language.

https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-myself/#api-rest-api-3-mypreferences-locale-get

Here is a snippet of my code:

 const getUserLanguage = async () => {
    try {
      const response = await requestJira("/rest/api/3/mypreferences/locale", {
        method: "GET",
        headers: {
          "Content-Type": "application/json",
        }
      });

      const userData = await response.json();
      const userLanguage = userData.locale; //Capturing the user language
      console.log(userLanguage);
      return userLanguage;
    } catch (error) {
      console.error("Error:", error);
      return null;
    }
  };

On my manifest.yml, the permissions are like that:

permissions:
  scopes:
    - read:jira-work
    - read:jira-user
    - write:jira-work
    - manage:jira-configuration

But nothing happens. No error is shown on the console or whatsoever. What is wrong on my implementation?

Thanks.

Hi @KnowCodeCompany,

I tried out your getUserLanguage and it works as expected.

When you say “console”, are you referring to the terminal when you run forge tunnel or the browser’s console? From your code snippet, I believe you are using Forge bridge’s requestJira function which means it is being called in the frontend and the log will be in the browser’s console.

Cheers,
Ian

Hey @ianRagudo, thanks for you reply!

Yes, I am referring to the browser’s console. I run this function and nothing happens.

How did you manage it?

Hi @KnowCodeCompany,

It probably is related to the way you are calling your async function getUserLanguage. One way I did it is this way, wherein I called your function in the front end:

const App = () => {
  const [language, setLanguage] = useState("");

  useEffect(async () => {
    await getUserLanguage().then((language) => setLanguage(language));
  }, []);

  return (
    <>
      <Text>My Language: {language}</Text>
    </>
  );
};

If you prefer calling the REST API in the resolver, then there are some tweaks to be done, like moving your function to the resolvers directory and calling it via Forge Bridge’s invoke, etc.

You can try this approach and let us know how it goes.

Cheers,
Ian

1 Like

Hey @ianRagudo, I see…

Now I’m having a problem with scopes:

async-forge-ui-issue…907.js:sourcemap:81 
 GET https://api.atlassian.com/ex/jira/817e8931-5fa2-4d29-9e68-4ac44174aa35/rest/api/3/mypreferences/locale 401 (Unauthorized)
index.jsx:34 
{code: 401, message: 'Unauthorized; scope does not match'}
code: 401
message: "Unauthorized; scope does not match"

and my scopes are:

permissions:
  scopes:
    - read:jira-work
    - read:jira-user
    - write:jira-work
    - manage:jira-configuration
    - read:user-configuration:jira
    - read:me

am I missing something?

Hi @KnowCodeCompany,

With a different error, it looks like we were able to get past the original concern.

You already have the scope in there —manage:jira-configuration. When you added the new scope, did you do a forge deploy and upgrade the installation by calling forge install —-upgrade so that the new scopes will be reflected?

Cheers,
Ian

1 Like

Hey @ianRagudo , hope you are well!

I’ve tried that but I’m facing errors and errors… really don’t know what can I do.

permissions:
  scopes:
    - manage:jira-configuration
    - read:jira-work
    - read:jira-user
    - write:jira-work
Error: Upgrade error: Authorization failed: Principal has insufficient permissions.
async-forge-ui-issue-panel.c366f907.js:81 
GET https://api.atlassian.com/ex/jira/817e8931-5fa2-4d29-9e68-4ac44174aa35/rest/api/3/mypreferences/locale 401 (Unauthorized)

This app of mine is also in production, and I just wanted to updated on the user language and upload it to production. But these things are not helping…

Hello @KnowCodeCompany.

For this error, you are potentially using an account that is not a contributor to the current Forge app you are updating (did you use a different account, perhaps?). Check this topic out for more information: Principal has insufficient permissions - #2 by ibuchanan

Cheers,
Ian

1 Like