CallBack后Twitter身份验证

编程入门 行业动态 更新时间:2024-10-25 13:18:14
本文介绍了CallBack后Twitter身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是我的代码:

我试图将twitter整合到我的应用程式,

public class OAuthForTwitter extends Activity { private CommonsHttpOAuthConsumer httpOauthConsumer; private OAuthProvider httpOauthprovider; public final static String consumerKey =xxxxxxxxxxxxxx; public final static String consumerSecret =xxxxxxxxxxxx; private final String CALLBACKURL =sosInternational:/// HierBenIkNu; private Twitter twitter; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); doOAuth(); } / ** *打开浏览器使用signpost jar和应用程序特定的 * consumerkey和consumerSecret。 * / private void doOAuth(){ try { httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret); httpOauthprovider = new DefaultOAuthProvider(twitter/oauth/request_token,twitter/oauth/access_token, twitter/oauth/authorize); String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer,CALLBACKURL); this.startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(authUrl))); } catch(Exception e){ Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show(); } } @Override protected void onNewIntent(Intent intent){ super.onNewIntent(intent); Uri uri = intent.getData(); if(uri!= null& uri.toString()。startsWith(CALLBACKURL)){ String verifier = uri .getQueryParameter(oauth.signpost.OAuth .OAUTH_VERIFIER); try { //这将填充消费者中的令牌和token_secret httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier); // TODO:你可能想要在你的存储token和token_secret //应用程序设置!!!!!!!! AccessToken a = new AccessToken(httpOauthConsumer.getToken(), httpOauthConsumer.getTokenSecret()); //初始化Twitter4J twitter = new TwitterFactory()。getInstance(); twitter.setOAuthConsumer(consumerKey,consumerSecret); twitter.setOAuthAccessToken(a); //创建tweet 日期d = new Date(System.currentTimeMillis()); String tweet =#OAuth working!+ d.toLocaleString(); //发送tweet twitter.updateStatus(tweet); } catch(Exception e){ Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show(); } } } }

当我在Twitter网站上完成验证后,应该将我重新导向应用程式。

但是我找不到这个网页:

我在我的AndroidManifest中有这个:

< intent-filter& < action android:name =android.intent.action.VIEW>< / action> < category android:name =android.intent.category.DEFAULT>< / category> < category android:name =android.intent.category.BROWSABLE>< / category> < data android:scheme =sosInternationalandroid:host =HierBenIkNu>< / data> < / intent-filter>

如何回到我的应用程序,我的密钥我回来?

$ b

我 < intent-filter> 不在应用程序中。

现在是这样:

< activity android:name =。OAuthForTwitter android:label =@ string / app_name android:configChanges =orientation | keyboardHidden android:launchMode =singleInstance> < intent-filter> < action android:name =android.intent.action.VIEW>< / action> < category android:name =android.intent.category.DEFAULT>< / category> < category android:name =android.intent.category.BROWSABLE>< / category> < data android:scheme =sosInternationalandroid:host =OAuthForTwitter>< / data> < / intent-filter> < / activity>

这种关闭工作,它只是从开始加载整个应用程序。

有没有办法只是返回到最后一个活动,而不重新启动整个应用程序?

I'm trying to integrate twitter to my app, but I can't seem to get it to work.

This is my code:

public class OAuthForTwitter extends Activity { private CommonsHttpOAuthConsumer httpOauthConsumer; private OAuthProvider httpOauthprovider; public final static String consumerKey = "xxxxxxxxxxxxxx"; public final static String consumerSecret = "xxxxxxxxxxxx"; private final String CALLBACKURL = "sosInternational:///HierBenIkNu"; private Twitter twitter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); doOAuth(); } /** * Opens the browser using signpost jar with application specific * consumerkey and consumerSecret. */ private void doOAuth() { try { httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret); httpOauthprovider = new DefaultOAuthProvider( "twitter/oauth/request_token", "twitter/oauth/access_token", "twitter/oauth/authorize"); String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, CALLBACKURL); this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl))); } catch (Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); } } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); Uri uri = intent.getData(); if (uri != null && uri.toString().startsWith(CALLBACKURL)) { String verifier = uri .getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER); try { // this will populate token and token_secret in consumer httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier); // TODO: you might want to store token and token_secret in you // app settings!!!!!!!! AccessToken a = new AccessToken(httpOauthConsumer.getToken(), httpOauthConsumer.getTokenSecret()); // initialize Twitter4J twitter = new TwitterFactory().getInstance(); twitter.setOAuthConsumer(consumerKey, consumerSecret); twitter.setOAuthAccessToken(a); // create a tweet Date d = new Date(System.currentTimeMillis()); String tweet = "#OAuth working! " + d.toLocaleString(); // send the tweet twitter.updateStatus(tweet); } catch (Exception e) { Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); } } } }

When I'm done authenticating on the Twitter site, it should redirect me back to the app.

But instead, I get this Page not found:

I have this in my AndroidManifest:

<intent-filter> <action android:name="android.intent.action.VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category> <category android:name="android.intent.category.BROWSABLE"></category> <data android:scheme="sosInternational" android:host="HierBenIkNu"></data> </intent-filter>

How can I go back to my app with the keys i get back?

解决方案

Ok, it was quite a dumb mistake.

My <intent-filter> wasn't inside an application..

This is how it is now:

<activity android:name=".OAuthForTwitter" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden" android:launchMode="singleInstance"> <intent-filter> <action android:name="android.intent.action.VIEW"></action> <category android:name="android.intent.category.DEFAULT"></category> <category android:name="android.intent.category.BROWSABLE"></category> <data android:scheme="sosInternational" android:host="OAuthForTwitter"></data> </intent-filter> </activity>

This kind off works, it just loads the whole app from start.

Isn't there a way to just 'go back' to the last activity without restarting the whole app?

更多推荐

CallBack后Twitter身份验证

本文发布于:2023-11-26 16:48:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   CallBack   Twitter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!