question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

issue with CORS on oauth flow on browser

See original GitHub issue

I cant make the oauth flow to work on the browser, the issue is very similar to this

I tried to follow those instructions but I think I am doing something wrong as I am still getting this CORS issue

my flow is as follow:

  1. creating oauth url:
function createAuth1() {
  console.log("creating auth")
    const authenticationUrl = snoowrap.getAuthUrl({
      clientId: 'client_id_from_reddit(installed app)',
      scope: ['identity', 'wikiread', 'wikiedit'],
      redirectUri: 'http://10.0.0.4:3000/',
      permanent: false,
      state: 'fe211bebc52eb3da9bef8db6e63104d3' // a random string, this could be validated when the user is redirected back
    });
    window.location = authenticationUrl
}

  1. after redirection extracting the code
const code = new URL(window.location.href).searchParams.get('code')
  1. creating instance where token is the extracted code
function createInstance(token) {
  return new snoowrap({
    userAgent:"Bot",
    clientId:"client_id_from_reeddit(installed app)",
    clientSecret:"",
    refreshToken:token,
  })
}
  1. trying to use it
const instance = createInstance(code)
console.log(await instance.getHot())

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
obiwankenoobicommented, Aug 29, 2019

Yes I changed permanent: false to permanent: true and I can see it now. Well It’s all looks good. Hope this is it! Thank you very much! (:

0reactions
charludocommented, Nov 21, 2021

Nevermind - figured it out. For anyone else with the same problem:

The auth-code used in fromAuthCode can only be used once.

Do Step 1 as above, then change step 2 to:

Snoowrap.fromAuthCode({
    code: code,
    userAgent: '<your user agent>,
    clientId: '<your client id>,
    clientSecret: '', //empty
    redirectUri: '<your redirect uri, same as in reddit application config>'
}).then(r => {
    const token = r.refreshToken; //this!! this is the refresh token!! not the code returned from reddit!!
    // do something with the token. I put it in localstorage.
    localStorage.setItem("refreshToken");
});

And now you can create a request object.

async function foo() {
    var token = await localStorage.getItem("refreshToken");
    const request = new Snoowrap({
        userAgent: '<your user agent>',
        clientId: '<your client id>',
        clientSecret: '', //empty
        refreshToken: token
    });
    return await session;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

CORS issue while getting token with Oauth 2.0 Client ...
CORS issue while getting token with Oauth 2.0 Client credential flow using just React? I am able to get the Bearer token from...
Read more >
Pitfalls Of OAuth2 and CORS - Medium
First, due to CORS restrictions, the server must respond with a status code other than a 302. This restriction is in place to...
Read more >
OAuth 2.0 Web Server Flow and CORS problem
You're using the wrong flow. You should be using the User-Agent flow, in which you redirect to a login URL, and when the...
Read more >
CORS issues with window.open() and OAuth2 - Stack Overflow
Short Answer: Check the Referer (spelling error is intentional) header in your request and response. New Chromium based browsers (Chrome, ...
Read more >
Why is my fetch request to OAuth server being blocked by ...
I ran into a bunch of problems with CORS and decided to do some ... Using any sort of fetch API from a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found