我正在开发一个使用登录到 Gmail 的应用程序。我浏览了所有教程、stackoverflow 问题,这些问题OAuth 2.0
在 google 中可用,最后按照 Google 官方文档中给出的指南OAuth 2.0
示例。我已经创建了客户端 ID 以及已安装的应用程序(即 Android)所需的一切。到目前为止一切正常,我打电话WebView
给用户授予许可,之后我Accesstoken
就像 4/cR7XXXXXXXXXXXXXXX
在WebView
说类似的话Please copy this code,switch to your application and paste it here
。我在这里很困惑,我不知道该怎么做,回到我的应用程序Webview
。我正在寻找大约两天的解决方案,但是,我无法为我的问题得到好的答案。这是我被阻止的代码。
Main.java
用户必须OptionMenu
登录的地方。当用户点击WebView
打开进入gmail。
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
public static String REQUEST = "https://accounts.google.com/o/oauth2/auth?"
+ "client_id=XXXXXXXXXXX-gxxxx.apps.googleusercontent.com&"
+ "redirect_uri=urn:ietf:wg:oauth:2.0:oob&"
+ "scope=https://mail.google.com/mail/feed/atom&"
+ "response_type=code&" + "access_type=online";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 0:
if (requestCode != RESULT_OK || data == null) {
return;
}
String token = data.getStringExtra("token");
if (token != null) {
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Intent intent_obj2 = new Intent(Main.this, Webview.class);
intent_obj2.setData(Uri.parse(REQUEST));
startActivityForResult(intent_obj2, 0);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Webview.java
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Webview extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
final WebView wb_obj = new WebView(this);
setContentView(wb_obj);
wb_obj.getSettings().setJavaScriptEnabled(true);
Intent intent = getIntent();
if (intent.getData() != null) {
wb_obj.loadUrl(intent.getDataString());
}
wb_obj.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
setProgress(newProgress * 10000);
}
});
wb_obj.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
setTitle(url);
System.out.println(url);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
System.out.println("in onPageFinsihed");
/*CookieSyncManager.getInstance().sync();
String s_cookie = CookieManager.getInstance().getCookie(url);
if (s_cookie == null) {
System.out.println(s_cookie);
return;
}else{
String web_title = wb_obj.getTitle().toString();
System.out.println("web tile" + web_title);
if (web_title.equalsIgnoreCase("Request for Permission")) {
} else {
String[] s_webtitle = web_title.split("=", 2);
String access_token = s_webtitle[1].toString();
//System.out.println("Access token" + access_token);
startActivity(new Intent(Webview.this, Main.class));
finish();
}*/
}
});
}
@Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(Webview.this, Main.class));
}
}
根据我调用的 Webview.java finish()
,当前活动被杀死,但这在这个应用程序中没有发生,所以我将获得令牌onAcitivityResult()
。请分享您的答案,谢谢。