2015-07-10 439 views
1

我正在构建一个连接到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活动重定向到主要活动?

回答

0

您在授权和令牌请求中都缺少一些请求参数。将身份验证网址更改为:

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 reference

+0

感谢您的回答,那是我错过了什么。在解释它之后,我甚至在API参考中找到它。猜猜我现在就要上床睡觉了。 – FuriousFry