oauthstackexchange-api

OAuth with StackExchange API


I'm trying to authenticate my user on my site using StackExchange OAuth API.

Everything works fine when the user signs up for the first time. At this point, I get the "access token", which I save in my DB.

But the next time the user signs in, access token value is actually different to the previous one. Is it supposed to be like this?

If then, how do I check for existing users who already have signed up using StackExchange?

I can get the user's information by giving a specific site name (e.g. 'stackoverflow'). But what if I don't have that information and I need to check that the user has already signed in via StackExchange?

Is there a "user" information that I can get given an accessToken?


Solution

  • The StackExchange OAuth implementation is not meant to authenticate users to 3rd party applications but instead it is about authorizing those 3rd party applications to get access to the StackExchange API.

    The access token that you get is not a token that represents a currently logged-in user, it represents the permission to access the API on behalf of the user gave it to your application. This is the classical confusion about the scope of OAuth 2.0 which is discussed in detail here: http://oauth.net/articles/authentication/.

    In summary: you can't use OAuth or the StackExchange access token to authenticate users.

    StackExchange does support the OpenID protocol to facilitate the purpose that you are looking for, see https://openid.stackexchange.com/. The downside of that is that OpenID is a deprecated protocol, superseded by OpenID Connect. OpenID Connect is an authentication protocol that is actually built on top of OAuth 2.0, but is not yet supported by StackExchange.