2017-02-10 3 views
3

Ich möchte auf MS Cloud API auf Android Flatform zugreifen.Zugriff auf MS Cloud-API nicht möglich

So fand ich eine Beispielanwendung.

Hier ist Beispiel App, die diese Funktion arbeiten. (https://github.com/adithya321/Companion-for-Band/blob/dev/app/src/main/java/com/pimp/companionforband/activities/cloud/WebviewActivity.java)

Ich habe MS Anwendung von dev.app.microsoft.com

und Passwort gemacht und Web-Plattform rediret uri registrieren (https://login.live.com/oauth20_desktop.srf)

So hatte ich meine client_id, client_serect

In der Funktion downloreUrl ist RequestMethod "GET". Also habe ich es in "POST" geändert.

private String downloadUrl(String url) throws IOException { 
    InputStream is = null; 
    try { 
     URL u = new URL(url); 
     HttpURLConnection conn = (HttpURLConnection) u.openConnection(); 
     conn.setReadTimeout(10000); 
     conn.setConnectTimeout(15000); 
     conn.setRequestMethod("GET"); 
     conn.setDoInput(true); 
     conn.connect(); 
     is = conn.getInputStream(); 

     return readIt(is, 9999); 
    } finally { 
     if (is != null) { 
      is.close(); 
     } 
    } 

aber es funktioniert nicht diese App.

Ich änderte Zugriffsmethode mit HttpClient, nicht HttpURLConnection.

Ich habe festgestellt, dass ich auf MS Cloud API mit öffentlichen Client zugreifen.

hier ist logcat

02-10 15:30:51.533 29336-29639/com.example.user.bandsample D/WebviewActivity: executeClient: {"error":"invalid_request","error_description":"Public clients can't send a client secret."} 

löschen client_secret, ich habe gerade ohne Aktualisierungs-Token access_token.

Ich weiß nicht, was ich tun soll.

Antwort

0

Ich löste das Problem.

Erstens: Wenn Sie die mobile App über das Anwendungscenter erstellt haben, benötigen Sie client_secret nicht.

Ich habe den Code URLConnection in HttpClient geändert. URLConnection ist einfacher, aber es funktioniert nicht.

diesen Code aus gradle android hinzufügen Httpclient für die Verwendung von

useLibrary 'org.apache.http.legacy' 

und Code downloadURL neu Code

public String executeClient() { 
     ArrayList<NameValuePair> post = new ArrayList<NameValuePair>(); 
     post.add(new BasicNameValuePair("client_id", getString(R.string.client_id))); 
     post.add(new BasicNameValuePair("redirect_uri", r_uri)); 
     // post.add(new BasicNameValuePair("client_secret", getString(R.string.client_secret))); 
     post.add(new BasicNameValuePair("code", code)); 
     post.add(new BasicNameValuePair("grant_type", "authorization_code")); 

     // 연결 HttpClient 객체 생성 
     HttpClient client = new DefaultHttpClient(); 


     // 객체 연결 설정 부분, 연결 최대시간 등등 
     HttpParams params = client.getParams(); 
     HttpConnectionParams.setConnectionTimeout(params, 5000); 
     HttpConnectionParams.setSoTimeout(params, 5000); 

     // Post객체 생성 
     HttpPost httpPost = new HttpPost("https://login.live.com/oauth20_token.srf"); 
     HttpGet httpget = new HttpGet("https://login.live.com/oauth20_token.srf"); 
     try { 
      UrlEncodedFormEntity entity = new UrlEncodedFormEntity(post, "UTF-8"); 
      // UrlEncodedFormEntity entity = new UrlEncodedFormEntity(post, "UTF-8"); 
      httpPost.setEntity(entity); 
      // Log.d(TAG, "executeClient: " + entity); 
      HttpResponse hr = client.execute(httpPost); 
      //HttpResponse hr = client.execute(httpget); 
      HttpEntity resEntitiy = hr.getEntity(); 
      String x = EntityUtils.toString(resEntitiy); 
      Log.d(TAG, "executeClient: " + x); 
      return x; 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

und ich StringRequest statt JsonObjectRequset

StringRequest tokenRequest = new StringRequest(Request.Method.POST, uurl, 
      new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      Log.d("반응1", response.toString()); 
      JsonAccessTokenExtractor jate = new JsonAccessTokenExtractor(); 
      accessToken = jate.extractAccessToken(response.toString()); 
      refreshToken = jate.extractRefreshToken(response.toString()); 
      MainActivity.editor.putString("access_token", accessToken); 
      MainActivity.editor.putString("refresh_token", refreshToken); 
      MainActivity.editor.apply(); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

      Log.d("반응1", error.toString()); 
      Log.d("반응1", ""+error.networkResponse.headers.toString()); 
     } 
    }){ 

     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("client_id", getString(R.string.client_id)); 
      params.put("redirect_uri", r_uri); 
      params.put("refresh_token", MainActivity.sharedPreferences.getString("refresh_token", "hi")); 
      params.put("grant_type", "refresh_token"); 
      Log.d(TAG, params.toString()); 
      return params; 
     } 

     @Override 
     public String getBodyContentType() { 
      return "application/x-www-form-urlencoded; charset=UTF-8"; 
     } 
    }; 
ändern
Verwandte Themen