无法使用有效访问密钥从Uber API获取令牌

时间:2015-05-24 23:24:45

标签: php curl oauth-2.0 uber-api

过去几天我一直试图将我的应用程序与超级版本集成,但由于某些原因,在oauth2身份验证期间,我无法获得有效的令牌。我可以获得访问代码,但是当使用curl时,无论我如何安排脚本,我似乎都无法获得访问令牌。这就是我所拥有的:

public class LoginFragment extends Fragment implements View.OnClickListener {

private static final String URL_SIGN_UP = "http://www.lovelup.net";
private static final long ANIMATION_LENGTH = 1000;
private static final String TAG = "LoginFragment";

private ViewPager viewPager;
private CirclePageIndicator circlePageIndicator;
private LinearLayout loginContainer;
private LoginButton fbLoginButton;
private LinearLayout activationContainer;
private CustomEditText etActivationCode;

private CallbackManager mCallbackManager;
private Activity mActivity;
private SharedPrefs mPrefs;
private FragmentManager mFm;
private String mEnteredCode;
private MixpanelHelper mMixpanelHelper;

public static LoginFragment newInstance() {
    return new LoginFragment();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_login, container, false);
    viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    circlePageIndicator = (CirclePageIndicator) view.findViewById(R.id.page_indicator);
    loginContainer = (LinearLayout) view.findViewById(R.id.login_container);
    fbLoginButton = (LoginButton) view.findViewById(R.id.fb_login_btn);
    ImageButton steamLoginButton = (ImageButton) view.findViewById(R.id.steam_login_btn);
    activationContainer = (LinearLayout) view.findViewById(R.id.activation_container);
    etActivationCode = (CustomEditText) view.findViewById(R.id.et_activation_code);
    CustomButton btnActivate = (CustomButton) view.findViewById(R.id.btn_activate);
    CustomButton btnAlreadyActivated = (CustomButton) view.findViewById(R.id.btn_already_activated);
    CustomTextView btnGetCode = (CustomTextView) view.findViewById(R.id.btn_get_code);

    ViewHelper.setupTouchListenerToHideKeyboard(view, getActivity());
    steamLoginButton.setOnClickListener(this);
    btnActivate.setOnClickListener(this);
    btnAlreadyActivated.setOnClickListener(this);
    btnGetCode.setOnClickListener(this);

    if (mActivity != null) Utils.clearSavedInfo(mActivity);
    setupActionBar();
    setupFacebookLoginButton();
    setupViewPager();
    setupKeyboard();
    showCorrectContainer();
    ((MainActivity)getActivity()).showCallToActionBanner();

    return view;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mActivity = activity;
}

@Override
public void onDestroy() {
    mActivity = null;
    super.onDestroy();
}

private void setupFacebookLoginButton() {
    FacebookHelper.getFacebookKeyHash(getActivity());
    fbLoginButton.setFragment(this);
    fbLoginButton.setReadPermissions(Arrays.asList("public_profile", "email", "user_birthday"));

    fbLoginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Log.i("Facebook", "Logged In");
            addLoadingFragment();
            FacebookHelper.requestUserInfo(mActivity, loginResult.getAccessToken(), new Runnable() {
                @Override
                public void run() {
                    if (mEnteredCode != null) {
                        Log.i(TAG, "Logging in with entered activation code");
                        new VolleyHelper(mActivity).mLogin(mEnteredCode);
                    }
                    else {
                        Log.i(TAG, "Logging in with existing account");
                        new VolleyHelper(mActivity).mLogin(null);
                    }
                }
            });
        }

        @Override
        public void onCancel() {
            Log.i("Facebook", "Login Cancelled");
        }

        @Override
        public void onError(FacebookException e) {
            Log.i("Facebook", "Error: " + e.getLocalizedMessage());
            ViewHelper.showCustomToast(getActivity(), e.getLocalizedMessage(), null);
            addLoginFragment();
        }
    });
}

我已经仔细检查了我的秘密和身份证,两者都是正确的。我可以看到,每次我获取访问代码时,它都是独一无二的,我可以看到它在我重定向的授权页面上回显,但不管我一直在哪里获取回复为:

<?php

echo $_GET['code']."<br>";
$token = curl_init();
$param = array(
    'client_secret' => 'MY_SECRET',
    'client_id' => 'MY_ID',
    'grant_type' => 'authorization_code',
    'code' => "{$_GET['code']}"
    );
$postData = '';
foreach($param as $k => $v)
    {
       $postData .= $k . '='.urlencode($v).'&';
    }
$postData = rtrim($postData, '&');
curl_setopt($token, CURLOPT_URL, 'https://login.uber.com/oauth/token');
curl_setopt($token, CURLOPT_HEADER, true);
curl_setopt($token, CURLOPT_RETURNTRANSFER, true);
curl_setopt($token, CURLOPT_POST, true);
curl_setopt($token, CURLOPT_POSTFIELDS, $postData);
$returned_token = curl_exec($token);
curl_close($token);
echo $returned_token;

?>

4 个答案:

答案 0 :(得分:2)

发生此错误的原因是:

  

重定向网址
  这些URL将在OAuth身份验证期间使用   如果您的请求中不包含重定向URI,则默认网址为   使用

  

redirect_uri(可选)
  我们将重定向到的URI   资源所有者的授权   URI的基础必须与应用程序注册期间使用的redirect_uri匹配。

答案 1 :(得分:0)

对我来说很好。确保import {HttpClient} from 'aurelia-http-client'; export class WebApi { static inject = [HttpClient]; constructor(http) { this.http = http; this.baseUrl = 'https://myProject.azure-mobile.net/'; this.zumoHeader = 'X-ZUMO-APPLICATION'; this.zumoKey = 'zumoKey'; this.client = new HttpClient() .configure(x => { x.withBaseUrl(this.baseUrl); x.withHeader(this.zumoHeader, this.zumoKey); }); } // requests will go here testGet() { this.client.jsonp('api/logs?application_id=sessionID&__count=20') .then(response => { alert(response); }); } } 标题正确无误。有些服务会期望这样。 (例如Content-Type

此外,您可以分享您Content-Type=x-www-form-urlencoded的请求方式吗? (例如,您在发送授权请求时使用的是code等)。

答案 2 :(得分:0)

我也遇到过这个问题,看起来我找到了它的原因。

尝试将 redirect_uri (与应用设置中的相同)添加到您的请求中,因此您的params代码应如下所示:

$param = array(
'client_secret' => 'MY_SECRET',
'client_id' => 'MY_ID',
'redirect_uri' => 'MY_REDIRECT_URI',
'grant_type' => 'authorization_code',
'code' => "{$_GET['code']}"
);

这对我有所帮助,但我希望优步开发人员能够使这个错误更加具体化

答案 3 :(得分:0)

它完美地对我有用。我的问题是,我在Dashboard应用程序上的Url Redirect与我发送给请求的参数之间存在差异。我修好了,一切正常