Uber Ride请求使用API

时间:2016-11-18 07:37:18

标签: android uber-api

我正在尝试使用Uber API集成优步登录和乘车请求。我已成功完成登录部分并尝试集成Ride Request部分。下面是我的代码片段 -

  SessionConfiguration config = new SessionConfiguration.Builder()
                // mandatory
                .setClientId("abc")
                // required for enhanced button features
                .setServerToken("xyz")
                // required for implicit grant authentication
                .setRedirectUri("https://login.uber.com/oauth/authorize")
                // required scope for Ride Request Widget features
                .setScopes(Arrays.asList(Scope.RIDE_WIDGETS, Scope.PROFILE, Scope.REQUEST))
                // optional: set Sandbox as operating environment
                .setEnvironment(SessionConfiguration.Environment.SANDBOX)
                .build();

        UberSdk.initialize(config);

        OAuth2Credentials credentials = new OAuth2Credentials.Builder()
                .setSessionConfiguration(config)
                **.build();**

        try {
            String authorizationUrl = credentials.getAuthorizationUrl();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Credential credential = credentials.authenticate(authorizationCode, userId); 
        CredentialsSession session = new CredentialsSession(config, credential);
        RidesService service = UberRidesApi.with(session).createService();

每当我运行它并尝试检查日志时,它会在OAuth的.build()行中崩溃。

这是我得到的错误 -

java.lang.IllegalStateException: Client ID and secret must be set.

Caused by: java.lang.IllegalStateException: Client ID and secret must be set.
                                                                                    at com.uber.sdk.rides.client.utils.Preconditions.checkState(Preconditions.java:66)
                                                                                    at com.uber.sdk.rides.auth.OAuth2Credentials$Builder.validate(OAuth2Credentials.java:170)
                                                                                    at com.uber.sdk.rides.auth.OAuth2Credentials$Builder.build(OAuth2Credentials.java:181)
                                                                                    at com.rahul.uberapi.android.demo.DemoActivity.onCreate(MainActivity.java:119)
                                                                                    at android.app.Activity.performCreate(Activity.java:5990)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
                                                                                    at android.app.ActivityThread.access$900(ActivityThread.java:154) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:135) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

但我已经设置了客户端ID和密码。

绝对与正在发生的事情混淆。请帮忙。

1 个答案:

答案 0 :(得分:0)

其中一个问题是你正在使用的重定向uri。这应该在开发人员仪表板中设置,它代表我们在成功授权应用程序后将用户重定向到的URL。请参阅以下示例:

if( (SeatCount+1) % 5 == 0 )

有关工作示例,请参阅相关的java api教程:https://developer.uber.com/docs/riders/ride-requests/tutorials/api/java