Gal*_*lip 2 java twitter android callback twitter4j
我正在尝试将twitter集成到我的应用程序中,但我似乎无法让它工作.
这是我的代码:
public class OAuthForTwitter extends Activity {
private CommonsHttpOAuthConsumer httpOauthConsumer;
private OAuthProvider httpOauthprovider;
public final static String consumerKey = "{removed}";
public final static String consumerSecret = "{removed}";
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(
"http://twitter.com/oauth/request_token",
"http://twitter.com/oauth/access_token",
"http://twitter.com/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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我在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="sosInternational" android:host="HierBenIkNu"></data>
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
如何使用我回来的钥匙回到我的应用程序?
好吧,这是一个非常愚蠢的错误.
我<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="sosInternational" android:host="OAuthForTwitter"></data>
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
这种关闭工作,它只是从一开始就加载整个应用程序.
有没有办法在没有重新启动整个应用程序的情况下"返回"上一个活动?
我解决了这个问题.不完全是你开发的方式,而是略有不同的方式.以下是描述我所做的步骤.
使用webview而不是在Web浏览器中打开它:这样做的一个关键优势是,您可以跟踪网址重定向.
setWebViewClient
你的webview的调用方法和shouldOverrideUrlLoading
你的类的覆盖方法,我已经使用了内部类.
您的方法中将包含url参数.检查它是否以您自己的回调URL开头,(注意:此URL包含授权所需的用户令牌和用户密码).
完成任务后,您可以隐藏活动或删除webView或任何您想要的内容.
编辑:这是Web应用程序中常用的oAuth方式,因此我们不需要xAuth方式.(如果其他社区成员不知道)
希望它会对你有所帮助.
归档时间: |
|
查看次数: |
6120 次 |
最近记录: |