3

For the Google Sign-In, I followed the sample code they had in the github. I was able to get a google sign in working. I got Auth code and ID token, but I didn't know how to setup a web server for getting the refresh token/access token so I used a work around.

                                // TODO(user): send code to server and exchange for access/refresh/ID tokens.
                                // [END get_auth_code]

                                OkHttpClient client = new OkHttpClient();
                                RequestBody requestBody = new FormEncodingBuilder()
                                        .add("grant_type", "authorization_code")
                                        .add("client_id", serverClientId)
                                        .add("client_secret", serverClientSecret)
                                        .add("redirect_uri","")
                                        .add("code", authCode)
                                        .add("id_token", idTokenString)
                                        .build();
                                final Request request = new Request.Builder()
                                        .url("https://www.googleapis.com/oauth2/v4/token")
                                        .post(requestBody)
                                        .build();
                                client.newCall(request).enqueue(new Callback() {
                                    @Override
                                    public void onFailure(final Request request, final IOException e) {
                                        Log.e("tag", e.toString());
                                    }

                @Override
                public void onResponse(Response response) throws IOException {
                    try {
                        JSONObject jsonObject = new JSONObject(response.body().string());
                        final String message = jsonObject.toString(5);
                        Log.i("tag", message);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

I was able to get the refresh and access token, but how do I create a credential for the Youtube API? I'm following the sample they have for upload video, and they use a completely different flow in the Auth class for credentials.

I found this from the documentation but I don't know what I need to set for the TokenResponse and what I'm supposed to use for the token server URL. Could someone clarify what to do next???

public static Credential createCredentialWithRefreshToken(
  HttpTransport transport, JsonFactory jsonFactory, TokenResponse tokenResponse) {
      return new Credential.Builder(BearerToken.authorizationHeaderAccessMethod()).setTransport(
    transport)
      .setJsonFactory(jsonFactory)
      .setTokenServerUrl(
        new GenericUrl("https://server.example.com/token"))
      .setClientAuthentication(new BasicAuthentication("s6BhdRkqt3", "7Fjfp0ZBr1KtDRbnfVdmIw"))
      .build()
      .setFromTokenResponse(tokenResponse);
  }
Community
  • 1
  • 1
selflearningcode
  • 738
  • 1
  • 7
  • 10

0 Answers0