【问题标题】:Redirect_URI for Android REST clientAndroid REST 客户端的 Redirect_URI
【发布时间】:2017-03-09 03:23:20
【问题描述】:

我正在 android 上构建一个 REST 客户端来与 Bitbucket API 进行通信。 我遇到了 OAUTH2.0 的问题。

  • 从Bitbucket提供的docs,你必须创建一个 "consumer" 以获取 ClientID(Key) 和 Secret 以用于 你的安卓应用
  • 在消费者创建表单中,有“回调 URL”(我理解为 Bitbucket 将 在用户完成授予应用程序的权限后将其带到 (即登录)
  • 在我的 android 应用程序中,我希望 android 设备在用户授予权限后返回我的应用程序(例如您如何使用 google 帐户登录网站,在 google 的权限页面上填写您的凭据后,您是被带回到原来的页面)

tutorial I was following 实现了这一点

  • 在 manifest.xml 中进行登录的活动中添加此项

<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="redirecturi" android:scheme="your" /> </intent-filter>

  • 将此发送到 Bitbucket

    private final String redirectUri = "your://redirecturi";
    
    @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("" + "/login" + "?client_id=" + clientId + "&redirect_uri=" + redirectUri));
                        Uri.parse("https://bitbucket.org/site/oauth2/authorize"  + "?client_id=" + clientId + "&redirect_uri=" + redirectUri));
                startActivity(intent);
            }
        });
    }
    

我的问题是: - 如何在 Bitbucket 消费者对象和我的应用程序的请求中配置重定向 URI 或回调 URL,以确保 android 设备返回到我的应用程序,以便我的应用程序可以捕获来自 Bitbucket 的响应(并且此响应应该具有我的 access_token需要)。

【问题讨论】:

    标签: android rest oauth-2.0 bitbucket-api


    【解决方案1】:

    管理此问题的一种简单方法是使用WebView,而不是通过意图打开您的 OAuth 链接。每当您在 WebView 中被重定向时,您实际上都可以检查 url。为此,您必须覆盖

    public boolean shouldOverrideUrlLoading(WebView view, String url)

    每当您被重定向到另一个链接(或者在这种情况下是您的回调 uri)时,您都可以检查或比较获得的 url 并相应地继续。

    【讨论】:

    • 谢谢。你能告诉我如何在我的特殊情况下实现这个吗?
    • 查看link上给出的示例
    • 首先使用 webview 获取 oauth 令牌,然后将其用于向 api 发出请求并保存在 sharedpreferences 中
    • 你可以在这个link找到更详细的解决方案。
    • 感谢您的帮助。我确实自己找到了不需要使用 WebView 的方法,但我认为你的方法是个好主意。
    猜你喜欢
    • 2012-01-06
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    相关资源
    最近更新 更多