如何在Google登录后创建凭据

时间:2016-11-17 02:00:50

标签: java android oauth-2.0 google-api android-youtube-api

对于Google登录,我按照github中的示例代码进行操作。我能够获得谷歌登录工作。我获得了Auth代码和ID令牌,但我不知道如何设置Web服务器以获取刷新令牌/访问令牌,因此我使用了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();
                    }
                }

我能够获得刷新和访问令牌,但是如何为Youtube API创建凭证?我跟踪他们上传视频的示例,他们在Auth class中使用了完全不同的流程来获取凭据。

我从文档中找到了这个,但我不知道我需要为TokenResponse设置什么以及我应该用于令牌服务器URL的内容。有人可以澄清下一步该做什么???

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);
  }

0 个答案:

没有答案