新来的机器人,练习3腿Oauth与FitBark API。我已经在Android Studio之外工作了。FitBark Oauth授权代码重定向停留在循环
- 第一站,Chrome浏览器,成功获取用户允许访问应用程序屏幕并获取授权代码。
- Second Leg,POSTMAN使用授权码,成功获取访问令牌。
- 第三条规则,使用终端中的curl和访问令牌,成功返回JSON和Get User Info API端点的用户配置文件信息。
不Android Studio中的工作:
我卡在首回合Android Studio中。我正在使用Retrofit Oauth tutorial。
我可以启动第一条腿,应用程序在浏览器中打开FitBark登录屏幕,我可以以用户身份登录。
一旦是一个完整的,浏览器重定向到另一个页面,并显示该授权码。
不过,我不希望它打开浏览器并显示该授权码。我希望它在登录/允许访问后重定向回应用程序。一旦它重新导向到我的应用程序,我的理解是我可以在Intent中捕获响应(我认为它是认证代码),并通过.getData()获取它。堆栈等地都指向添加特定的代码到Android清单,在意图过滤器,在我发起的第一站呼叫活动
改装指南和各种来源。
的FitBark REDIRECT_URI与我的凭据来了:
urn:ietf:wg:oauth:2.0:oob
设置为FITBARK_REDIRECT
恒定在我登录活动(在的onCreate)进行测试,以获取授权代码:
public void getAuthCode(){
Intent intent = new Intent(
Intent.ACTION_VIEW,
Uri.parse(ServiceGenerator.FITBARK_BASE_URL + "/authorize?response_type=code&client_credentials&" + "client_id=" +FITBARK_CLIENT_ID + "&redirect_uri=" + FITBARK_REDIRECT));
startActivity(intent);
}
我的onResume在登录活动中,应该从Intent获取数据。此代码确实会触发,但我从getIntent.getData()
收到空值。
@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){
Log.d("On RESUME", "Gets Here");
}
if (uri != null && uri.toString().startsWith(intentRedirect)) {
// use the parameter your API exposes for the code (mostly it's "code")
String code = uri.getQueryParameter("code");
Log.d("CODE", code);
if (code != null) {
// get access token
// we'll do that in a minute
} else if (uri.getQueryParameter("error") != null) {
Log.d("ERROR URI", "ERROR ERROR ERROR");
}
}
}
我的Android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.macbook.retrofit">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="app.open"
android:scheme="myapp" />
</intent-filter>
</activity>
</application>
</manifest>
从阅读指南,我的理解是android:host
和android:scheme
需要与我在FitBark第一支路呼叫REDIRECT_URI匹配。所以,我已经试过了由加载的myapp://app.open”的号召
Uri.parse(ServiceGenerator.FITBARK_BASE_URL + "/authorize?response_type=code&client_credentials&" + "client_id=" +FITBARK_CLIENT_ID + "&redirect_uri=" + "myapp://app.open"));
startActivity(intent);
加入之前。‘MYAPP://app.open’,我将其设置为自定义穿过FitBark的API重定向终点站。
curl -X POST -H "Authorization: Bearer <My Token Value goes Here>" -H
"Content-Type: application/json" -d
'{"redirect_uri":"urn:ietf:wg:oauth:2.0:oob\rmyapp://app.open"}'
"https://app.fitbark.com/api/v2/redirect_urls"
循环:
当我点击(上主)按钮启动登录活动中,getAuthCode()在登录的OnCreate火灾。浏览器打开,然后它陷入循环。
所以,那就是我所在的位置。我想获得授权码并在第二段中使用它,但我无法弄清楚。请帮忙!!!