0
我正在開發一款應用程序,該應用程序應該用於從用戶YouTube帳戶獲取分析數據。 我可以使用我的應用程序中的webview中的下面的url檢索Google的授權碼。使用OAuth2訪問Android中的YouTube API
https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/yt-analytics.readonly&approval_prompt=force&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=762546229188.apps.googleusercontent.com&access_type=offline
我從頁面標題中檢索代碼,然後使用Java的substring方法切斷成功。 然而,當我去獲得訪問令牌只會返回"error" : "invalid_grant"
HttpClient client = new DefaultHttpClient(); //new HTTP client
HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/token"); //where to post to
List<NameValuePair> pairs = new ArrayList<NameValuePair>(); //construct the values to hand to Google
Log.v("KUZMA", arg0[0]); //personal logcat message
pairs.add(new BasicNameValuePair("code", arg0[0])); //in arg0[0] lives the auth code
pairs.add(new BasicNameValuePair("client_id", CLIENT_ID)); //I store the Client_id as a static variable at the beginning of my class
//Log.v("Kuzma", CLIENT_ID);
//pairs.add(new BasicNameValuePair("client_secret", "*******"));
pairs.add(new BasicNameValuePair("redirect_uri", "http://localhost"));
pairs.add(new BasicNameValuePair("grant_type", "authorization_code")); //Leave this line how it is
String responseBody = null;
try {
post.setEntity(new UrlEncodedFormEntity(pairs));
org.apache.http.HttpResponse response = client.execute(post);
responseBody = EntityUtils.toString(response.getEntity());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.v("KUZMA", responseBody); // That just logs it into logCat
//output.setText("something happened?");
return null;
所以我想我最大的問題是,我沒有一個客戶端機密,但它不是由谷歌爲提供給我安裝應用程序有沒有解決的辦法?我也玩過Google Play服務,但無法實現這一功能,但是如果您知道這種方式的效果會非常好!
是的,我應該回來說我想出來了,但是5點左右轉了一圈,我完成了:)我正在使用GoogleAuthUtil與AccountPicker一起生成和使用令牌。謝謝。另外,我在標題和標籤中提到了Android。我認爲這很清楚,但再次感謝。 – jskuzma