gpt4 book ai didi

android - 使用 WebView 登录 Twitter

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:46 26 4
gpt4 key购买 nike

您好,我使用浏览器完成了 twitter 登录

代码

TwitLoginActivity.java(主要 Activity )

package com.app.twitlogin;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TwitLoginActivity extends Activity {

static String TWITTER_CONSUMER_KEY;
static String TWITTER_CONSUMER_SECRET;
static String TWITTER_CALLBACK_URL;
static String URL_TWITTER_OAUTH_VERIFIER;

private static Twitter twitter;

Button btnActualLoginTwitter;

private static RequestToken requestToken;

String twitUserID, twitUserName, twitAccessToken, twitTokenSecret, twitProfileImg;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twit_login);

if (android.os.Build.VERSION.SDK_INT > 11) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
}

TWITTER_CONSUMER_KEY = getString(R.string.twitter_consumer_key);
TWITTER_CONSUMER_SECRET = getString(R.string.twitter_consumer_secret);
TWITTER_CALLBACK_URL = getString(R.string.twitter_callback);
URL_TWITTER_OAUTH_VERIFIER = getString(R.string.twitter_oauth_verifier);

btnActualLoginTwitter = (Button) findViewById(R.id.btn_twit_login);

btnActualLoginTwitter.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {

btnActualLoginTwitter.setClickable(false);

Runnable buttonClickable = new Runnable() {

@Override
public void run() {
btnActualLoginTwitter.setClickable(true);
}
};

Handler pdCanceller = new Handler();
pdCanceller.postDelayed(buttonClickable, 2000);

Log.e("Came", "Came");

loginToTwitter();

}
});

Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL)) {

String verifier = uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);

try {
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);

twitUserID = "" + twitter.getId();
twitUserName = twitter.getScreenName();
twitAccessToken = accessToken.getToken();
twitTokenSecret = accessToken.getTokenSecret();

User user = twitter.showUser(twitter.getId());
twitProfileImg = user.getProfileImageURL();

Log.e("twitUserID", twitUserID);
Log.e("twitUserName", twitUserName);
Log.e("twitAccessToken", twitAccessToken);
Log.e("twitTokenSecret", twitTokenSecret);
Log.e("twitProfileImg", twitProfileImg);

} catch (Exception e) {
Log.e("Twitter Login Error", "" + e.getMessage());
}

}
}

private void loginToTwitter() {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
builder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);
Configuration configuration = builder.build();

TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();

Log.e("TwitterLoginObject", twitter + "");

try {
requestToken = twitter.getOAuthRequestToken(TWITTER_CALLBACK_URL);
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL())));

} catch (TwitterException e) {
e.printStackTrace();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.twit_login, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Strings.xml

<string name="twitter_consumer_key">May Key</string>
<string name="twitter_consumer_secret">My secret</string>
<string name="twitter_callback">oauth://t4jsample</string>
<string name="twitter_oauth_verifier">oauth_verifier</string>

list

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.twitlogin"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />

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

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

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".TwitLoginActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />

<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />

<data
android:host="t4jsample"
android:scheme="oauth" />
</intent-filter>


</activity>
</application>

</manifest>

现在我想使用 WebView 而不是浏览器登录(为什么因为有时 twitter 不会重定向到我的应用程序)。对于这个要求,我在谷歌和 SO 中看到了几乎所有的建议,但我没能实现。所以,请帮助我使用 WebView 登录

最佳答案

链条 1:

在 loginToTwitter() 中这样做:

if (!isLoggedIn) {
final ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(consumerKey);
builder.setOAuthConsumerSecret(consumerSecret);

final Configuration configuration = builder.build();
final TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();

try {
requestToken = twitter.getOAuthRequestToken(callbackUrl);

final Intent intent = new Intent(getActivity(), WebViewActivity.class);
intent.putExtra(WebViewActivity.EXTRA_URL, requestToken.getAuthenticationURL());
startActivityForResult(intent, WEBVIEW_REQUEST_CODE);

} catch (TwitterException e) {
e.printStackTrace();
}
}

第 2 步:

并通过在 WebViewActivity 中扩展 WebViewClient 创建 webviewclient,如 here

第 3 步:

并在您的主要 Activity 中实现“onActivityResult”,例如:

if (requestCode == WEBVIEW_REQUEST_CODE) {
try {
String verifier = data.getExtras().getString(oAuthVerifier);
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);

long userID = accessToken.getUserId();
final User user = twitter.showUser(userID);
String username = user.getName();

saveTwitterInfo(accessToken);
String id = mSharedPreferences.getString(WSKeys.TwitterClass.PREF_USER_ID, "");
String name = mSharedPreferences.getString(WSKeys.TwitterClass.PREF_USER_NAME, "");
wscallSocialSignIn("", id, accessToken.toString(), name, image, "Twitter");
} catch (Exception exception) {

}
}

关于android - 使用 WebView 登录 Twitter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34449673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com