我正在構建一個連接到Misfit的API以收集數據並進行一些科學研究的移動android應用程序。 (https://build.misfit.com/)Misfit的API使用的OAuth授權方法證明有點困難。OAuth:缺少參數response_type
我得到儘可能按一個按鈕打開一個網頁視圖,以不稱職的授權頁面,然後我可以登錄我我登錄後,web視圖產生以下錯誤:
{"error":"invalid_request","error_description":"Missing required parameter: response_type"}
我對issueing該請求代碼如下: 的想法是張貼令牌並獲得訪問代碼,他們都儲存在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);
}
注意:我在網上發現了這個代碼, t是我作爲新應用程序開發人員最容易理解的代碼之一。如果上面的代碼被證明是錯誤的(或者我對它的理解),請仍然沒有給出任何解釋,請糾正我。另外:如何在登錄後將OAuth活動重定向到主要活動?
感謝您的回答,那是我錯過了什麼。在解釋它之後,我甚至在API參考中找到它。猜猜我現在就要上牀睡覺了。 – FuriousFry