Authorize youtube data api request using google sign in api

别说谁变了你拦得住时间么 提交于 2019-12-11 07:57:02

问题


i am trying to authorize a youtube data request (scope) using google sing in api. Google Sign In works perferct inside my app and i am getting logged in. How do I use the result of the google sign in api to send a request to a youtube data api scope?

My Sign In:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestScopes(new Scope("https://www.googleapis.com/auth/youtube"))
            .build();
    api = new GoogleApiClient.Builder(main.getApplicationContext())
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

This workes fine.

I get a GoogleSignInAccount and a GoogleApiClient from the result. How do I send a request to youtube for example to add a subscription or get the channel name/id?

Thanks

Keba


回答1:


ask Google is not able to work with/answer my question, I will now do it myself. (It´s a little embarrassing that a company like google is not able to help developers who ARE working with there apis.)

But thats what I got to work:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestScopes(new Scope("https://www.googleapis.com/auth/youtube"))
            .requestIdToken("APIKEY FROM WEBSITE")
            .requestEmail()
            .build();
    api = new GoogleApiClient.Builder(main.getApplicationContext())
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
    api.connect();

This will authorise the user, if you want android to show a login window:

Intent intent = Auth.GoogleSignInApi.getSignInIntent(api);
            main.startActivityForResult(intent, 9001);

The result can be used as following:

Account account = result.getSignInAccount().getAccount();

The Account can now be used to e.g. subscribe to somebody on youtube.

GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
            Main.main,
            Collections.singleton("https://www.googleapis.com/auth/youtube"));
    credential.setSelectedAccount(google.getAccount());
    youTube = new YouTube.Builder(
            new NetHttpTransport(),
            new JacksonFactory(),
            credential)
            .setApplicationName("APPNAME")
            .build();

The youtube object can now be used for anything you like.

Thanks for this community, even when i am very disappointed about google.

Keba



来源:https://stackoverflow.com/questions/43638589/authorize-youtube-data-api-request-using-google-sign-in-api

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