我正在遵循本指南:https://developers.google.com/identity/sign-in/android/
提示我输入我的Google用户,我登录确定,我可以访问youtube数据API。
但我希望它能促使我选择我的链接品牌帐户。这可能吗?我从nodejs应用程序开始工作,但在这种情况下它似乎不受支持。
答案 0 :(得分:1)
原来你不能。目前尚不支持。
相反,您需要仅使用youtube范围通过https://github.com/openid/AppAuth-Android/,这会正确显示您的频道/品牌帐户。
然后在youtube api中使用结果我在AppAuth TokenActivity中执行了此操作:
/**
* Define a global instance of the HTTP transport.
*/
public static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
/**
* Define a global instance of the JSON factory.
*/
public static final JsonFactory JSON_FACTORY = new JacksonFactory();
private YouTube mYoutube;
@MainThread
private void fetchUserInfo(String accessToken, String idToken, AuthorizationException ex) {
if (ex != null) {
Log.e(TAG, "Token refresh failed when fetching user info");
mUserInfoJson.set(null);
runOnUiThread(this::displayAuthorized);
return;
}
mYoutube = new YouTube.Builder(TokenActivity.HTTP_TRANSPORT, TokenActivity.JSON_FACTORY, new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest httpRequest) throws IOException {
httpRequest.getHeaders().setAuthorization("Bearer " + accessToken);
}
})
.build();
mExecutor.submit(() -> {
try {
YouTube.LiveBroadcasts.List list = mYoutube.liveBroadcasts()
.list("id, snippet, contentDetails, status")
.setMine(true);
Log.i(TAG, "List to string " + list.toString());
LiveBroadcastListResponse response = list
.execute();
JSONObject obj = new JSONObject();
obj.put("name", response.getItems().get(0).getSnippet().getTitle());
mUserInfoJson.set(obj);
} catch (IOException e) {
Log.e(TAG, "Failed to construct user info endpoint URL", e);
} catch (JSONException e) {
Log.e(TAG, "Failed to set name", e);
// e.printStackTrace();
}
runOnUiThread(this::displayAuthorized);
});