【问题标题】:Can't get back from browser to app无法从浏览器返回到应用程序
【发布时间】:2016-07-16 16:19:06
【问题描述】:

我正在尝试授权我的 GitHub 应用程序并返回我的应用程序。我使用 Intent 打开授权页面,但无法从浏览器返回我的应用程序。你能看看我的代码,告诉我哪里出错了吗?

LoginActivity.java

private final String clientId="myclientid";
private final String clientSecret="myclientsecret";
private final String redirectUri="http://localhost";
private final String state="randomshit";

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

    Button loginButton = (Button) findViewById(R.id.loginbutton);
    loginButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("https://github.com/login/oauth/authorize?" +
                            "&client_id="+clientId+
                           "&redirect_uri="+redirectUri+
                            "&state="+state)
                    );
            startActivity(intent);
        }
    });
}


@Override
protected void onResume() {
    super.onResume();

    // the intent filter defined in AndroidManifest will handle the return from ACTION_VIEW intent
    Uri uri = getIntent().getData();

    if (uri != null && uri.toString().startsWith(redirectUri)) {
        String code = uri.getQueryParameter("code");

        if (code != null) {
            Log.d("code",code);
        }
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.figengungor.retrogithub">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW"></action>
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data
                    android:host="localhost"
                    android:scheme="http" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的 GitHub 应用和重定向 uri:

【问题讨论】:

  • can't go back to my app from browser 是什么意思?你得到的回应是什么?
  • 按下手机中的“返回键”会发生什么?
  • 授权后重定向到我的redirectUri,并且code参数附加到我的redirectUri,表示授权成功。据说如果 redirectUri 与我添加的意图过滤器中的重定向匹配,它应该返回到应用程序,然后我的 onResume 部分提取代码参数。不应该这样工作吗?
  • 当按钮被点击时,首先会生成URL。网址是:“github.com/login/oauth/authorize?” + "&client_id="+clientId+ "&redirect_uri="+redirectUri+ "&state="+state;然后既然你用 Intent 说 startActivity(intent)。看法;它将打开浏览器并转到上述 URL。这就对了。之后代码中就没有什么可做的了。

标签: android api github authorization


【解决方案1】:

您使用“http”和“localhost”作为意图过滤器是否有原因? 我认为您的问题是浏览器将始终处理重定向,因为它理解协议(http)。 我会使用不同的协议和主机名。例如:

    <activity android:name=".LoginActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW"></action>
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data
                android:host="consumecode"
                android:scheme="oauth" />
        </intent-filter>
    </activity>

确保在 GitHub 上将授权回调 URL 更新为“oauth://consumecode”。

【讨论】:

    【解决方案2】:

    只需使用您自己的浏览器即可完成。 表示使用 webview 登录您的应用程序。它很简单,我认为它会解决您的问题。

    【讨论】:

    • 我知道这种替代方法,但我想了解为什么它不适用于这种方法。
    【解决方案3】:

    尝试在您的 loginButton onClick 中使用 startActivityForResult(intent) 而不是 startActivity(intent)

    【讨论】:

      猜你喜欢
      • 2011-07-25
      • 2016-12-29
      • 2018-11-12
      • 2016-12-29
      • 1970-01-01
      • 2012-08-21
      • 1970-01-01
      • 2015-02-16
      • 1970-01-01
      相关资源
      最近更新 更多