Search code examples
flutterdartmobile-applicationuber-api

Uber api in flutter mobile application


I am working on a Flutter mobile application in which users can request a ride for transportation from one place to other. I am using uber-API for that purpose. I have implemented the deep link to open the uber app from my flutter application. But my client wants me to show the real-time updates of fare and distance within the application and then open the uber application if the user wants.

I have read the uber-API documentation but I am unable to achieve that. Any help regarding this issue will be appreciated.


Solution

  • You need Access Token via Authorization Code

    Integration Steps

    Step 1: Select the scopes from the above list. Your selection will be saved for later

    Step 2:First, the user has to grant your app permission to access their data or do actions on their behalf. Uber provides an authentication page where users can securely sign in with their Uber username and password to grant permissions to your app. This authorization page is accessible through the below authorization URL.

    https://login.uber.com/oauth/v2/authorize?client_id=<CLIENT_ID>&response_type=code&redirect_uri=<REDIRECT_URI>
    

    Step 3: Once the Uber user authenticates and authorizes your app, Uber will issue an HTTP 302 redirect to the redirect_uri passed in Step 1 (or the first redirect URI in the dashboard if none was explicitly provided in Step 1). On that redirect, you will single-use authorization code which will expire in 10 minutes. The code query param is the authorization code needed for step 4.

    GET https://your-redirect-uri/?code=<AUTHORIZATION_CODE> Step 4: Use the endpoint below to exchange the authorization code for an access_token which will allow you to make request on behalf of the user. The access_token is good for a limited period of time described by the expires_in field (in seconds) in response.

    Request
    curl -F 'client_secret=<CLIENT_SECRET>'\
          -F 'client_id=<CLIENT_ID>'\
          -F 'grant_type=authorization_code'\
          -F 'redirect_uri=<REDIRECT_URI>'\
          -F 'code=<AUTHORIZATION_CODE_FROM_STEP_2>'\
          "https://login.uber.com/oauth/v2/token"
    Respone
    {
        "access_token": "xxx",
        "token_type": "Bearer",
        "refresh_token": "xxx",
        "scope": "profile history offline_access"
    }
    

    Step 5: You can pass the access_token returned in step 4 as a bearer token in the Authorization header, or pass it as a query parameter in the URL. See the example below of OAuth sent in the header. Replace <ACCESS_TOKEN> below with the token from Step 4.

    curl -H "Authorization: Bearer <ACCESS_TOKEN>"\
        https://api.uber.com/v1.2/products?latitude=37.7759792-logitude=-122.41823