简体   繁体   中英

How do I get access token from Foursquare?

I get code from this direct url with my client id and redirect uri; https://foursquare.com/oauth2/authenticateclient_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI

But I can't do it with the rest service. I have to embed this service in my java application and then get access token. I can use that different option,if there is another way you know to get access token.Can you help me?

The Foursquare docs walk through the process in great detail. There are 2 options:

  • Web Applications Code Flow
  • Web Applications Token Flow

Both these options will require you to setup an app through the Foursquare Developer site . You'll need to setup a redirect URL for Foursquare to redirect back to. This is usually a publically accessible URL, but a locahost URL also works for testing purposes.

The first, the Code Flow, follows a standard OAuth process:

  1. Direct users (generally done through a link or button) to

     https://foursquare.com/oauth2/authenticate?client_id=YOUR_CLIENT_ID&response_type=code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI` 
  2. If the user accepts, they will be redirected back to

     https://YOUR_REGISTERED_REDIRECT_URI/?code=CODE 
  3. Your server should exchange the code it got in step 2 for an access token. Make a request for

     https://foursquare.com/oauth2/access_token?client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&grant_type=authorization_code&redirect_uri=YOUR_REGISTERED_REDIRECT_URI&code=CODE 
  4. The response will be JSON

     { access_token: ACCESS_TOKEN } 

This access token is what you're looking for.

The second method, the token flow is slightly easier:

  1. Redirect users who wish to authenticate to

     https://foursquare.com/oauth2/authenticate?client_id=CLIENT_ID&response_type=token&redirect_uri=YOUR_REGISTERED_REDIRECT_URI 
  2. If a user accepts, they will be redirected back to

     https://YOUR_REGISTERED_REDIRECT_URI/#access_token=ACCESS_TOKEN 

This access_token query param is what you're looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM