【问题标题】:Android: LinkedIn OAuth Callback not WorkingAndroid:LinkedIn OAuth 回调不起作用
【发布时间】:2012-07-23 21:55:20
【问题描述】:

我在我的 Android 应用中使用 LinkedIn OAuth。我已经有一个 LinkedIn 应用程序、消费者密钥和秘密,因此我可以成功请求。

在回调之前一切都很好。网页没有回调,我的意思是onNewIntentonResume 方法没有调用。网页只显示带有参数的回调 url。我的意思是它看起来像:

callback_url://?oauth_token=324sdf&oath_verifier=as21dsf

这是我的所有代码:

try {
    consumer = new CommonsHttpOAuthConsumer("ConsumerKey", "ConsumerSecret");
provider = new CommonsHttpOAuthProvider("https://api.linkedin.com/uas/oauth/requestToken", "https://api.linkedin.com/uas/oauth/accessToken", "https://api.linkedin.com/uas/oauth/authorize");
    final String url = provider.retrieveRequestToken(consumer, Constants.OAUTH_CALLBACK_URL);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_FROM_BACKGROUND);
    startActivity(intent);                          
} catch (Exception e) {
    e.printStackTrace();
}

活动已在Manifest 中定义为singleInstance

出了什么问题或遗漏了什么?

【问题讨论】:

    标签: android oauth


    【解决方案1】:

    经过长时间的研究,我自己找到了答案。

    我已将我的基类更改为linkedin-j,可以查看here

    然后设置这个常量如下:

        public static final String CONSUMER_KEY = "ConsumerKey";
        public static final String CONSUMER_SECRET = "ConsumerSecret";
        public static final String OAUTH_CALLBACK_SCHEME = "callback";
        public static final String OAUTH_CALLBACK_URL = OAUTH_CALLBACK_SCHEME + ":///";
    

    然后像这样初始化:

    LinkedInOAuthService oAuthService = LinkedInOAuthServiceFactory.getInstance().createLinkedInOAuthService(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
    LinkedInApiClientFactory factory = LinkedInApiClientFactory.newInstance(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
    LinkedInRequestToken liToken;
    LinkedInApiClient client;
    
    liToken = oAuthService.getOAuthRequestToken(Constants.OAUTH_CALLBACK_URL);
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(liToken.getAuthorizationUrl()));
    startActivity(i);
    

    这个回调很好,我已经在 OnNewIntent 处理了:

    String verifier = intent.getData().getQueryParameter("oauth_verifier");
    
    LinkedInAccessToken accessToken = oAuthService.getOAuthAccessToken(liToken, verifier);
    client = factory.createLinkedInApiClient(accessToken);
    client.postNetworkUpdate("Test");
    

    就是这样。

    【讨论】:

    • 嗨马丁,我有很多搜索和演示测试,所有示例都失败了,现在在你的帖子中,我想先知道,你的代码是否适用于 android 4.0 及更高版本?
    • 您能否发布更新,您的代码是否适用于 Android 4.0 以上版本。除此之外,请分享您的 AndroidManifest.xml,因为我在输入详细信息并在 LinkedIn WebView 上按 Enter 后收到 ERR_UNKNOWN_URL_SCEME。
    猜你喜欢
    • 1970-01-01
    • 2014-03-24
    • 2014-11-30
    • 1970-01-01
    • 2021-04-16
    • 2015-10-22
    • 2014-09-13
    • 2013-11-20
    • 2018-04-08
    相关资源
    最近更新 更多