3

谁能告诉我从我的 Android 应用程序执行 twitter 登录的 Open Auth 的正确步骤?还有一件事是可以在我的应用程序中对 Twitter 登录页面进行 ngng 的情况下执行 Twitter 帐户的登录身份验证吗?

Idea 是使用 twitter 帐户登录我的 android APP(身份验证)吗?

我已经尝试过了,但它是 twitter webview 进行身份验证而无需在我的应用程序中输入用户名和密码?

所以请帮助我!提前致谢 !!

4

3 回答 3

8

应用程序设置

Create App here https://developer.twitter.com/en/apps/create

Add Callback URLs to twittersdk:// (For Android SDK)

From App Details goto Keys and tokens and add in res/values/strings.xml

<string name="twitter_api_key">REPLACE_KEY</string>
<string name="twitter_api_secret">REPLACE_SECRET</string>

From App Details goto Permissions -> Edit

Access permission -> Read, write, and Direct Messages
Additional permissions -> Check to true (Request email address from users)
Save

在 AndroidManifest.xml 中添加 INTERNET 权限

<uses-permission android:name="android.permission.INTERNET" />

将 twitter SDK 依赖添加到 build.gradle (Module:app)

dependencies {
    implementation 'com.twitter.sdk.android:twitter:3.1.1'
    //implementation 'com.twitter.sdk.android:twitter-core:3.1.1'
    //implementation 'com.twitter.sdk.android:tweet-ui:3.1.1'
}

活动中

private TwitterAuthClient twitterAuthClient;

Custom Button Click

TwitterConfig config = new TwitterConfig.Builder(this)
    .logger(new DefaultLogger(Log.DEBUG))
    .twitterAuthConfig(new TwitterAuthConfig(getResources().getString(R.string.twitter_api_key), getResources().getString(R.string.twitter_api_secret)))
    .debug(true)
    .build();
Twitter.initialize(config);

twitterAuthClient = new TwitterAuthClient();

TwitterSession twitterSession = TwitterCore.getInstance().getSessionManager().getActiveSession();

if (twitterSession == null) {
    twitterAuthClient.authorize(this, new Callback<TwitterSession>() {
        @Override
        public void success(Result<TwitterSession> result) {
            TwitterSession twitterSession = result.data;
            getTwitterData(twitterSession);
        }

        @Override
        public void failure(TwitterException e) {
            Log.e("Twitter", "Failed to authenticate user " + e.getMessage());
        }
    });
} else {
    getTwitterData(twitterSession);
}

private void getTwitterData(final TwitterSession twitterSession) {
    TwitterApiClient twitterApiClient = new TwitterApiClient(twitterSession);
    final Call<User> getUserCall = twitterApiClient.getAccountService().verifyCredentials(true, false, true);
    getUserCall.enqueue(new Callback<User>() {
        @Override
        public void success(Result<User> result) {

            String socialId = "", firstName = "", lastName = "", gender = "", birthday = "", email = "", picture = "";

            User user = result.data;
            socialId = user.idStr;
            email = user.email;
            /*picture = user.profileImageUrlHttps.replace("_normal", "");
            firstName = user.name;
            lastName = user.screenName;*/

            try {
                firstName = user.name.split(" ")[0];
                lastName = user.name.split(" ")[1];
            } catch (Exception e) {
                firstName = user.name;
                lastName = "";
            }

            Log.e("Twitter", "SocialId: " + socialId + "\tFirstName: " + firstName + "\tLastName: " + lastName + "\tEmail: " + email);
        }

        @Override
        public void failure(TwitterException exception) {
            Log.e("Twitter", "Failed to get user data " + exception.getMessage());
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (twitterAuthClient != null) {
        twitterAuthClient.onActivityResult(requestCode, resultCode, data);
    }
}
于 2019-05-23T07:28:36.180 回答
0

您必须将Twitter OAuth用于您的应用程序的身份验证。
示例twitter OAuth描述here

于 2012-08-23T12:57:00.473 回答
0

它帮助我将 Twitter 集成到 android 应用程序中。

这是适用于 Android 的官方 Twitter 工具包(Twitter SDK)

检查此链接https://dev.twitter.com/twitterkit/android/installation

dependencies {
// Include all the Twitter APIs
compile 'com.twitter.sdk.android:twitter:3.0.0'
// (Optional) Monetize using mopub
compile 'com.twitter.sdk.android:twitter-mopub:3.0.0'
}

repositories {
  jcenter()
 }

public class CustomApplication {
  public void onCreate() {
   TwitterConfig config = new TwitterConfig.Builder(this)
       .logger(new DefaultLogger(Log.DEBUG))
       .twitterAuthConfig(new TwitterAuthConfig("CONSUMER_KEY",   "CONSUMER_SECRET"))
    .debug(true)
 .build();
Twitter.initialize(config);
}
}

在您的布局中,使用以下代码添加一个登录按钮:

<com.twitter.sdk.android.core.identity.TwitterLoginButton
 android:id="@+id/login_button"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

在活动中

loginButton = (TwitterLoginButton) findViewById(R.id.login_button);
loginButton.setCallback(new Callback<TwitterSession>() {
 @Override
 public void success(Result<TwitterSession> result) {
    // Do something with result, which provides a TwitterSession for making API calls
 }

 @Override
 public void failure(TwitterException exception) {
     // Do something on failure
  }
 });

@Override
protected void onActivityResult(int requestCode, int resultCode,   Intent data) {
  super.onActivityResult(requestCode, resultCode, data);

  // Pass the activity result to the login button.
  loginButton.onActivityResult(requestCode, resultCode, data);
}
于 2017-06-14T13:35:20.873 回答