Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从OAuth2.1迁移到OAuth2.0_Java_Gwt_Oauth_Oauth 2.0_Google Oauth - Fatal编程技术网

Java 从OAuth2.1迁移到OAuth2.0

Java 从OAuth2.1迁移到OAuth2.0,java,gwt,oauth,oauth-2.0,google-oauth,Java,Gwt,Oauth,Oauth 2.0,Google Oauth,我有一个GWT web应用程序。我有一张谷歌代币和代币秘密的大桌子。 我想转到OAuth2.0,而不需要重新验证用户。我在Java测试中尝试了以下内容 String url = "https://accounts.google.com/o/oauth2/token"; HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(url); String generatedStr

我有一个GWT web应用程序。我有一张谷歌代币和代币秘密的大桌子。 我想转到OAuth2.0,而不需要重新验证用户。我在Java测试中尝试了以下内容

  String url = "https://accounts.google.com/o/oauth2/token";

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);

    String generatedString = Utils.generateRandomString(10);
    String key = new Date().getTime() + "";

    // add header
    post.setHeader("Host", "accounts.google.com");
    post.setHeader("Content-Type", "application/x-www-form-urlencoded");
    post.setHeader("Authorization", "OAuth " +
            "oauth_consumer_key=\"" + CONSUMER_KEY + "\"," +
            "oauth_token=\"" + token + "\"," +
            "oauth_signature_method=\"HMAC-SHA1\"" +
            "timestamp=\"" + key +  "\"," +
            "oauth_nonce=\"" + generatedString + "\"," +
            "oauth_signature=\"" + calculateRFC2104HMAC(key, generatedString) + "\"," +
            "oauth_signature_method=\"HMAC-SHA1\"");

    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();

    urlParameters.add(new BasicNameValuePair("grant_type", "urn:ietf:params:oauth:grant-type:migration:oauth1"));
    urlParameters.add(new BasicNameValuePair("client_id", CONSUMER_KEY));
    urlParameters.add(new BasicNameValuePair("client_secret", CONSUMER_SECRET));
    urlParameters.add(new BasicNameValuePair("oauth_consumer_key", CONSUMER_KEY));
    urlParameters.add(new BasicNameValuePair("oauth_consumer_secret", CONSUMER_SECRET));
    urlParameters.add(new BasicNameValuePair("oauth_signature_method", "HMAC-SHA1"));
    urlParameters.add(new BasicNameValuePair("oauth_timestamp", "" + new Date().getTime()));
    urlParameters.add(new BasicNameValuePair("oauth_token", token));
    urlParameters.add(new BasicNameValuePair("oauth_token_secret", token));

    post.setEntity(new UrlEncodedFormEntity(urlParameters));

    HttpResponse response = client.execute(post);

    System.out.println("Sending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + post.getEntity());
    System.out.println("Response Code : " + response.getStatusLine().getStatusCode());

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

    StringBuffer result = new StringBuffer();
    String line;

    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    System.out.println(result.toString());
stringurl=”https://accounts.google.com/o/oauth2/token";
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(url);
String generatedString=Utils.generateRandomString(10);
字符串键=新日期().getTime()+“”;
//添加标题
post.setHeader(“主机”、“accounts.google.com”);
post.setHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
post.setHeader(“授权”、“OAuth”+
“oauth\u consumer\u key=\”“+consumer\u key+”,”+
“oauth\u令牌=\”“+令牌+\”,”+
“oauth\u签名\u方法=\“HMAC-SHA1”+
“时间戳=\”“+key+”\,”+
“oauth\u nonce=\”“+生成字符串+”\,”+
“oauth\u签名=\”“+计算器C2104HMAC(键,生成字符串)+”“\”,”+
“oauth\u签名\u方法=\“HMAC-SHA1\”;
List urlParameters=new ArrayList();
add(新的BasicNameValuePair(“grant_type”,“urn:ietf:params:oauth:grant type:migration:oauth1”);
添加(新的BasicNameValuePair(“客户机id”,消费者密钥));
添加(新的BasicNameValuePair(“客户机密”,消费者机密));
添加(新的BasicNameValuePair(“oauth_consumer_key”,consumer_key));
添加(新的BasicNameValuePair(“oauth_consumer_secret”,consumer_secret));
添加(新的BasicNameValuePair(“oauth_签名_方法”,“HMAC-SHA1”);
添加(新的BasicNameValuePair(“oauth_timestamp”,“new Date().getTime()));
添加(新的BasicNameValuePair(“oauth_token”,token));
添加(新的BasicNameValuePair(“oauth_token_secret”,token));
setEntity(新的UrlEncodedFormEntity(urlParameters));
HttpResponse response=client.execute(post);
System.out.println(“向URL发送‘POST’请求:“+URL”);
System.out.println(“Post参数:+Post.getEntity());
System.out.println(“响应代码:+Response.getStatusLine().getStatusCode());
BufferedReader rd=新的BufferedReader(新的InputStreamReader(response.getEntity().getContent());
StringBuffer结果=新的StringBuffer();
弦线;
而((line=rd.readLine())!=null){
结果。追加(行);
}
System.out.println(result.toString());
但我仍然没有得到任何充分的回应。我只得到:

响应代码:400

{
“error”:“invalid_request”,“error_description”:“invalid authorization header”。
}您需要根据

有关生成基字符串时的一些有用提示,请参见

POST正文应仅包含3个参数,如下所示

grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Amigration%3Aoauth1&
client_id=8819981768.apps.googleusercontent.com&
client_secret=xxxxxxxxxx