在Android的WebView中授权Twitter时,是否有可能从WebView中检索到PIN码?
我已经有了教程示例,但是用户需要将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();
}
}
}
});
}
然后在对话框弹出窗口中显示 Webview:
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();
}
我甚至不记得我从哪里拿到这个例子的。但是要摆脱最后一个对话框会很好。