是否有可能以某種方式從Android中的WebView中檢索PIN碼,同時授權Twitter?Twitter - 從webview中檢索pin碼。 Android
我有教程的例子,但登錄用戶需要重寫從WebView到應用程序的PIN碼。我只是想通過這個並直接從網上獲取PIN碼。
這和它在iPhone上的表現非常相似。
現在我使用的東西是這樣的:
public void getTwitter()
{
handleEvent.post(new Runnable()
{
@Override
public void run()
{
if (oHelper.hasAccessToken())
{
oHelper.configureOAuth(twitterConnection);
try
{
twitterConnection.updateStatus("Hii!!!!");
}
catch (TwitterException e)
{
Log.d("TWEET", "Error Updating status " + e.getMessage());
e.printStackTrace();
}
}
else
{
try
{
twitterConnection.setOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
requestToken = twitterConnection.getOAuthRequestToken("");
webViewDialog(requestToken.getAuthorizationURL(), 0);
}
catch (TwitterException e)
{
e.printStackTrace();
}
}
}
});
}
然後顯示對話框彈出裏面的網頁流量:
private void webViewDialog(final String authorizationURL, final int type)
{
LinearLayout container = new LinearLayout(this);
container.setMinimumWidth(200);
container.setMinimumHeight(320);
webView = new WebView(this);
webView.setMinimumWidth(200);
webView.setMinimumHeight(380);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl(authorizationURL);
container.addView(webView);
webDialog = new AlertDialog.Builder(this);
webDialog.setView(container).setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dia = dialog;
if (type == 0)
{
twitterPinCodeDialog();
}
}
}).show();
}
,並在最後提示用戶輸入PIN碼:
public void twitterPinCodeDialog()
{
LinearLayout pinHolder = new LinearLayout(this);
pinHolder.setGravity(Gravity.CENTER);
final EditText editPinCode = new EditText(this);
editPinCode.setGravity(Gravity.CENTER);
editPinCode.setHint("Pin Code");
editPinCode.setWidth(200);
pinHolder.addView(editPinCode);
Builder pinBuilder = new AlertDialog.Builder(this);
pinBuilder.setView(pinHolder).setTitle("Twitter Pin Code Entry")
.setMessage("Please enter displayed twitter code into the field")
.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if (editPinCode.getText().toString() != null && !editPinCode.getText().toString().equals(""))
{
try
{
accessToken = twitterConnection.getOAuthAccessToken(requestToken,editPinCode.getText().toString());
oHelper.storeAccessToken(accessToken);
Log.i("Access Token:", accessToken.getToken());
Log.i("Access Secret:", accessToken.getTokenSecret());
twitterConnection.updateStatus("Tweeted Successfully");
}
catch (TwitterException te)
{
if (401 == te.getStatusCode())
{
System.out.println("Unable to get the access token.");
}
else
{
te.printStackTrace();
}
}
}
else
{
Toast.makeText(context, "Pin code is required",
Toast.LENGTH_SHORT).show();
dialog.dismiss();
dia.dismiss();
twitterPinCodeDialog();
}
}
}).setNegativeButton("Cancel",new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog,int which)
{
Toast.makeText(context,"To share your news please complete login next time",Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}).show();
}
我甚至不記得我從哪裏拿這個例子。 但最好擺脫最後的對話。
謝謝,我會試試看。如果你找到一些例子,你可以跟我分享一下嗎? – goodm 2012-02-22 10:18:31