Access YouTube brand account using GoogleSignInApi

丶灬走出姿态 提交于 2021-02-19 23:31:50

问题


I am following this guide: https://developers.google.com/identity/sign-in/android/

Which prompts me for my Google user and I login ok, I can access the youtube data api.

However I want it to prompt me to choose my linked brand account instead. Is this possible? I had it working from a nodejs app but it doesn't seem supported in this case.


回答1:


Turns out you can't. Just not supported at this time.

Instead you need to go via https://github.com/openid/AppAuth-Android/ using just the youtube scope, this prompts for your channels/brand accounts correctly.

Then to use the result with the youtube api I did this in the 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);
    });


来源:https://stackoverflow.com/questions/44040198/access-youtube-brand-account-using-googlesigninapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!