uber requestRide()方法响应错误401:未经授权

时间:2017-10-13 13:34:23

标签: android uber-api

当我尝试在Uber api上调用 requestRide()方法时,我收到错误。响应正文为空,响应消息为"未授权"错误代码401;而onResponse()回调方法正在执行,但onFailure()回调方法没有执行。

这是我调用requestRide()并实现回调接口的代码......

private void requestForNewRide(RidesService service, int position){
        RideRequestParameters rideRequestParameters = new RideRequestParameters.Builder().setPickupCoordinates(PICKUP_LATITUDE, PICKUP_LONGITUDE)
                .setProductId(productIds.get(position))
                .setFareId(fareIds.get(position))
                .setDropoffCoordinates(DROPOFF_LATITUDE, DROPOFF_LONGITUDE)
                .build();
        service.requestRide(rideRequestParameters).enqueue(new Callback<Ride>() {
            @Override
            public void onResponse(Call<Ride> call, Response<Ride> response) {

                if (response.isSuccessful()) {
                    Toast.makeText(CustomActivity.this, "Request ride success", Toast.LENGTH_SHORT).show();

                    try {
                        //ride details
                        String rideId = response.body().getRideId();
                        String rideStatus = response.body().getStatus();
                        Integer rideEta = response.body().getEta();                           //estimated time of arrival in min
                        Float rideSurgeMultiplier = response.body().getSurgeMultiplier();     //rise in price
                        Driver rideDriver = response.body().getDriver();
                        Location rideLocation = response.body().getLocation();
                        Vehicle rideVehicle = response.body().getVehicle();

                        //ride driver details
                        String driverName = rideDriver.getName();
                        String driverPhoneNumber = rideDriver.getPhoneNumber();
                        String driverPictureUri = rideDriver.getPictureUrl();
                        Float driverRating = rideDriver.getRating();

                        //ride Location details
                        Float rideLocationLatitude = rideLocation.getLatitude();
                        Float rideLocationLongitude = rideLocation.getLongitude();
                        Integer rideLocationBearing = rideLocation.getBearing();

                        //ride Vehicle details
                        String rideVehicleLicencePlate = rideVehicle.getLicensePlate();
                        String rideVehicleMake = rideVehicle.getMake();
                        String rideVehicleModel = rideVehicle.getModel();
                        String rideVehiclePictureUrl = rideVehicle.getPictureUrl();

                        //Log
                        Log.d("uberridedetails", "rideId: " + rideId);
                        Log.d("uberridedetails", "rideStatus: " + rideStatus);
                        Log.d("uberridedetails", "rideEta: " + rideEta);
                        Log.d("uberridedetails", "rideSurgeMultiplier: " + rideSurgeMultiplier);
                        Log.d("uberridedetails", "driverName: " + driverName);
                        Log.d("uberridedetails", "driverPhoneNumber: " + driverPhoneNumber);
                        Log.d("uberridedetails", "driverPictureUri: " + driverPictureUri);
                        Log.d("uberridedetails", "driverRating: " + driverRating);
                        Log.d("uberridedetails", "rideLocationLatitude: " + rideLocationLatitude);
                        Log.d("uberridedetails", "rideLocationLongitude: " + rideLocationLongitude);
                        Log.d("uberridedetails", "rideLocationBearing: " + rideLocationBearing);
                        Log.d("uberridedetails", "rideVehicleLicencePlate: " + rideVehicleLicencePlate);
                        Log.d("uberridedetails", "rideVehicleMake: " + rideVehicleMake);
                        Log.d("uberridedetails", "rideVehicleModel: " + rideVehicleModel);
                        Log.d("uberridedetails", "rideVehiclePictureUrl: " + rideVehiclePictureUrl);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else {
                    Toast.makeText(CustomActivity.this, "Error: "+response.message(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Ride> call, Throwable t) {
                Toast.makeText(CustomActivity.this, "Failed to request ride", Toast.LENGTH_SHORT).show();
            }
        });
    }

我已经检查过param productId和FareId是有效的,我从api本身得到了(通过调用estimateRide()我得到Price对象,我得到了fareId。并且通过在RideService对象上调用getProducts()我有产品ID。

这是设置代码......

 SessionConfiguration config = new SessionConfiguration.Builder()
                .setClientId(getResources().getString(R.string.client_id))
                .setRedirectUri(getResources().getString(R.string.redirect_url))
                .setEnvironment(SessionConfiguration.Environment.SANDBOX)
                .setScopes(Arrays.asList(Scope.PROFILE, Scope.RIDE_WIDGETS, Scope.REQUEST, Scope.REQUEST_RECEIPT))
                .build();
        UberSdk.initialize(config);

而且......

LoginCallback loginCallback = new LoginCallback() {
            @Override
            public void onLoginCancel() {
                // User canceled login
                Toast.makeText(CustomActivity.this, "User canceled login", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onLoginError(@NonNull AuthenticationError error) {
                // Error occurred during login
                Toast.makeText(CustomActivity.this, "Error occurred during login", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onLoginSuccess(@NonNull AccessToken accessToken) {
                // Successful login!  The AccessToken will have already been saved.
                Toast.makeText(CustomActivity.this, "Successful login!  The AccessToken will have already been saved.", Toast.LENGTH_SHORT).show();
                createSession();
            }
            @Override
            public void onAuthorizationCodeReceived(@NonNull String authorizationCode) {
                Toast.makeText(CustomActivity.this, "Authorization code received", Toast.LENGTH_SHORT).show();
                createSession();
            }
        };
        AccessTokenManager accessTokenManager = new AccessTokenManager(getApplicationContext());
        LoginManager loginManager = new LoginManager(accessTokenManager, loginCallback);
        loginManager.setRedirectForAuthorizationCode(true);
        loginManager.login(this);
        mAccessTokenManager = accessTokenManager;
        mLoginManager = loginManager;

注1:这些是我正在使用的范围...

Scope.PROFILE, Scope.RIDE_WIDGETS, Scope.REQUEST, Scope.REQUEST_RECEIPT

注2:我正在使用我的开发者帐户登录。

如果我应该提及任何其他细节,请告诉我。

1 个答案:

答案 0 :(得分:0)

我绝对已经找到了自己的解决方案。

我注意到只有在使用GENERAL SCOPE时才会调用onLoginSuccess()回调方法。

每当我使用RESTRICTED SCOPE时,都不会调用该方法,而是调用另一个名为onAuthorizationCodeReceived()的回调方法。

然后我发现, 每当调用onAuthorizationCodeReceived()方法时,AccessTokenManager对象中都没有保存访问令牌 。因此,如果没有访问令牌,当我尝试请求乘坐时错误返回&#34;未授权&#34;。

因此,我试图弄清楚如何使用授权代码生成Access令牌。我在Android部分找不到关于此过程的文档。然后我在REST Web服务api中找到了解决方案。

以下是我的答案LINK ......

注意: Uber Doc中没有提及onAuthorizationCodeReceived()的回调方法。