Android Twitter 登录按钮不起作用 [英] Android Twitter Login button not working
问题描述
我正在编写一个包含使用 Twitter 按钮登录的应用程序.按钮没问题.当我单击 I give auth 时,它会显示在屏幕上,但我无法将令牌和机密保存到字符串变量中.我的意思是它总是空的.下面的代码是由 fabric.io 自动创建的,我在其中添加了一些东西,但它不起作用.没有错误,它只是不更改变量令牌和秘密.我已经通过一个 TextView 对象尝试了这个,该对象在成功案例中发生变化并且它永远不会改变.我应该怎么办?我只是想保存token和secret以便下次获取用户信息.
I'm writing an application that includes a sign in with twitter button. The button is okay. It shows on the screen when I click I give auth but I cant save the token and secret to a string variable. I mean it is empty always. The code at the below has been created by fabric.io automatically and I have added some stuff in it but it is not working. There is no error it just doesn't change the variable token and secret. I have tried this by an TextView object that changes in the success case and it never changes. What should I do? I just want to save token and secret to get user's information another time.
loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
System.out.println("Okay we are logged in now!");
TwitterSession session = Twitter.getSessionManager().getActiveSession();
TwitterAuthToken authToken = session.getAuthToken();
token = authToken.token;
secret = authToken.secret;
String answers = "";
User user = new User(userid,token,secret,answers);
registerUser(user);
twitterauth = true;
}
@Override
public void failure(TwitterException exception) {
// Do something on failure
}
});
如果我再次单击使用 Twitter 按钮登录,则会出现如下错误:
EDİT: I'm taking an error like the following, if I click the sign in with Twitter button again:
2660-2660/com.website.nameofmyapp E/Twitter﹕ Authorization completed with an error
com.twitter.sdk.android.core.TwitterAuthException: Authorize failed.
at com.twitter.sdk.android.core.identity.TwitterAuthClient.handleAuthorize(TwitterAuthClient.java:110)
at com.twitter.sdk.android.core.identity.TwitterAuthClient.authorize(TwitterAuthClient.java:101)
at com.twitter.sdk.android.core.identity.TwitterLoginButton$LoginClickListener.onClick(TwitterLoginButton.java:161)
at android.view.View.performClick(View.java:5184)
at android.view.View$PerformClick.run(View.java:20910)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
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:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
我想要做的就是在成功"自动发生时开始一个新的活动.
All I want to do is simple to start a new activity when "success" happens automatically.
推荐答案
两点帮助了我 -
1- 在 MainActivity.java 中添加这个
1- In MainActivity.java add this
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
FragmentManager fragment = getSupportFragmentManager();
if (fragment != null) {
fragment.findFragmentByTag("TwitterLogin").onActivityResult(requestCode, resultCode, data);
}
else Log.d("Twitter", "fragment is null");
}
2- 在你的片段类中添加这个-
2- In your fragment class add this-
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
loginButton.onActivityResult(requestCode, resultCode, data);
}
这篇关于Android Twitter 登录按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!