[英]OAuth: Missing parameter response_type
我正在構建一個連接到Misfit的API的移動android應用程序,以收集數據並進行一些科學研究。 ( https://build.misfit.com/)Misfit的API使用OAuth授權方法,事實證明這有點困難。
我只要按一下按鈕即可將WebView打開到Misfit的“授權”頁面,然后可以在其中登錄。登錄后,Webview會產生以下錯誤:
{"error":"invalid_request","error_description":"Missing required parameter: response_type"}
我發出該請求的代碼如下:想法是對令牌進行POST並獲取訪問代碼,將它們都存儲在SharedPreferences中,以便並非每個應用程序啟動都需要新的登錄
public class OAuthActivity extends Activity {
public static String OAUTH_URL = "https://api.misfitwearables.com/auth/dialog/authorize";
public static String OAUTH_ACCESS_TOKEN_URL = "https://api.misfitwearables.com/auth/tokens/exchange";
public static String CLIENT_ID = "Here's a client ID";
public static String CLIENT_SECRET = "and the secret, that obviously stays hidden.";
public static String CALLBACK_URL = "http://localhost";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_auth_o);
String url = OAUTH_URL + "?client_id=" + CLIENT_ID;
WebView webview = (WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
final SharedPreferences prefs = this.getSharedPreferences(
"com.iss_fitness.myapplication", Context.MODE_PRIVATE);
webview.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String URL, Bitmap favicon) {
String accessTokenFragment = "access_token=";
String accessCodeFragment = "code=";
// We hijack the GET request to extract the OAuth parameters
if (url.contains(accessTokenFragment)) {
// the GET request contains directly the token
String accessToken = url.substring(url.indexOf(accessTokenFragment));
prefs.edit().putString("Token", accessToken);
} else if(url.contains(accessCodeFragment)) {
// the GET request contains an authorization code
String accessCode = url.substring(url.indexOf(accessCodeFragment));
prefs.edit().putString("Code", accessCode);
String query = "&client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&code=" + accessCode;
view.postUrl(OAUTH_ACCESS_TOKEN_URL, query.getBytes());
}
}
});
webview.loadUrl(url);
}
注意:我在網上找到了此代碼,這是我作為新應用程序開發人員最容易理解的代碼之一。 仍然沒有任何解釋,如果以上代碼證明是錯誤的(或我的理解),請糾正我。 另外:登錄后,如何獲得OAuth活動將我重定向到主要活動?
您在授權和令牌請求中都缺少一些請求參數。 將身份驗證網址更改為:
String url = String url = OAUTH_URL+ "?response_type=code" +"&client_id=" + CLIENT_ID+ "&redirect_uri=" + CALLBACK_URL + "&scope=" + SCOPE;
其中,SCOPE是逗號分隔的權限字符串,例如“ public,birthday,email”。
並且,將令牌請求參數更改為:
String query = "grant_type=authorization_code" + "&client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&code=" + accessCode+ "&redirect_uri=" + CALLBACK_URL;
有關更多詳細信息,請參考Misfit API參考
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.