gpt4 book ai didi

microsoft-band - 无法访问 MS 云 API

转载 作者:行者123 更新时间:2023-12-01 14:44:49 27 4
gpt4 key购买 nike

我想在 android 平台上访问 MS cloud api。

所以我找到了一个示例应用。

这是使用此功能的示例应用程序。 ( https://github.com/adithya321/Companion-for-Band/blob/dev/app/src/main/java/com/pimp/companionforband/activities/cloud/WebviewActivity.java )

我从 dev.app.microsoft.com 制作了 MS 应用程序

并设置密码并注册网络平台rediret uri( https://login.live.com/oauth20_desktop.srf )

所以我有我的 client_id,client_serect

在downloreUrl 函数中,RequestMethod 是“GET”。因此,我将其更改为“POST”。

    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();
}
}

但它不适用于此应用。

我使用 HttpClient 更改了访问方法,而不是 HttpURLConnection 。

我发现我可以通过公共(public)客户端访问 MS cloud api。

这里是日志

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."}

删除client_secret,我只是得到了没有刷新 token 的access_token。

我不知道该怎么办。

最佳答案

我解决了这个问题。

首先,如果您从应用中心制作移动应用程序,则不需要 client_secret。

我将代码 URLConnection 更改为 HttpClient。URLConnection 更简单,但它不起作用。

从 gradle android 添加此代码以使用 HttpClient

useLibrary 'org.apache.http.legacy'

并将代码 Downloadurl 更改为新代码

   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;
}

我使用 StringRequest 而不是 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";
}
};

关于microsoft-band - 无法访问 MS 云 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42154018/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com