1

Google Plus API を Web アプリケーションに統合しようとしています。

Google で認証でき、コードを取得できます。access_tokenusing を取得しようとすると、問題が発生しHttpClientます。

ここにいくつかのコードがあります:

DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(
                "https://accounts.google.com/o/oauth2/token");
        post.addHeader("accept", "application/json");
        post.addHeader("Content-Type", "application/x-www-form-urlencoded");

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("code", myCode));
        nvps.add(new BasicNameValuePair("client_id",
                "my_client_id"));
        nvps.add(new BasicNameValuePair("redirect_uri",
                "http://localhost:8080/test/gplus.do"));
        nvps.add(new BasicNameValuePair("client_secret",
                "my_client_secret"));
        nvps.add(new BasicNameValuePair("grant_type", "authorization_code"));
        post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

        HttpResponse response = httpClient.execute(post);

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (response.getEntity().getContent())));

        String output;
        while ((output = br.readLine()) != null) {
            res = res + output;
        }

以前に何度かモック アプリケーションでトークンを取得できましたが、現在は不明な理由で失敗しているようです。私が得るのはすべてですerror:invalid_grant。なぜこうなった?

4

1 に答える 1

1

以前にこの方法でトークンを取得したと確信していますか? 私の知る限り、oAuth では、最初に /authorize を呼び出して認証コードを取得し、次にこのコードを使用して /token を呼び出してアクセス トークンを取得します。

psパッケージをSpring for oAuthとして使用できるため、すべての「舞台裏」のものを作成し、XMLを構成するだけです。

于 2012-10-24T07:04:02.907 に答える