2013-04-22 1 views
1

jersey-client를 사용하여 REST 호출을하고 있습니다. 응답으로 SignatureDoesNotMatch 오류가 발생합니다. GET 서비스를 사용하여 Bucket 이름을 나열하려고 시도했으나 GET Bucket 메서드를 사용하여 Bucket 객체를 나열하려고했습니다. 여기 내 샘플 코드입니다.Google Cloud Storage - Java REST API - SignatureDoesNotMatch 받기

힌트 또는 해결책?

public class restSample { 

    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1"; 
    private static final String PROJECT_ID = "10XXXXXXXX478"; 

    public static String Base64Encoding() 
      throws java.security.SignatureException, UnsupportedEncodingException { 

      String access_id = "GOOGBAXXXXXXXXXXBI"; 
      String secret_key = URLEncoder.encode("pWTXXXXXXXXXXXXXXXRo85T+XXXXXXXXX3O","UTF-8"); 
      String bucket = "bucket_name"; 

      String version_header = "x-goog-api-version:1"; 
      String project_header = "x-goog-project-id:"+PROJECT_ID; 
      String canonicalizedResources = "/"+bucket+"/"; 

      Calendar calendar = Calendar.getInstance(); 
      calendar.add(Calendar.MINUTE, 30); 
      long expiration = calendar.getTimeInMillis(); 

     String stringToSign = URLEncoder.encode("GET\n\n\n"+expiration+"\n"+version_header+"\n"+project_header+"\n"+canonicalizedResources,"UTF-8"); 
     //String stringToSign = URLEncoder.encode("GET\n\n\n"+getdate()+"\n"+version_header+"\n"+project_header+"\n"+canonicalizedResources,"UTF-8"); 
     String authSignature=""; 
     try { 

      SecretKeySpec signingKey = new SecretKeySpec(secret_key.getBytes(),HMAC_SHA1_ALGORITHM); 

      Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM); 
      mac.init(signingKey); 

      // compute the hmac on input data bytes 
      byte[] rawHmac = mac.doFinal(stringToSign.getBytes("UTF-8")); 

      // base64-encode the hmac 
      authSignature = new String(Base64.encode(rawHmac)); 

     } catch (Exception e) { 
      throw new SignatureException("Failed to generate HMAC : " + e.getMessage()); 
     } 
     authSignature = (access_id +":"+ authSignature); 
     return authSignature; 
    } 

    public static void main(String[] args) { 

     ClientConfig config = new DefaultClientConfig(); 
     Client client = Client.create(config); 

     String authSignature = null; 
     try { 
      authSignature = "GOOG1 "+ Base64Encoding(); 
     } catch (SignatureException e) { 
      e.printStackTrace(); 
     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } 

     WebResource service = client.resource(getBaseURI()); 
     ClientResponse response = service.accept(MediaType.APPLICATION_XML) 
       .header("Authorization",authSignature) 
       .header("Date", getdate()) 
       .header("Content-Length", "0") 
       .header("x-goog-api-version", "1") 
       .header("x-goog-project-id", PROJECT_ID) 
       .get(ClientResponse.class); 

     System.out.println(response.getClientResponseStatus().getFamily()); 
     System.out.println("response1 :: " + response.getEntity(String.class)); 

    } 

    private static URI getBaseURI() { 
     String url = "https://bucket_name.storage.googleapis.com"; 
     return UriBuilder.fromUri(url).build(); 
    } 
    private static String getdate(){ 
     SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z ", new Locale("US")); 
     Calendar cal = Calendar.getInstance(new SimpleTimeZone(0, "GMT")); 

     format.setCalendar(cal); 
     return format.format(new Date()); 
    } 
} 

고마워요!

답변

1

서명하려는 문자열이 서명 할 예상 문자열과 일치하는지 확인하십시오. Google Cloud Storage는 인증에 실패하면 예상되는 문자열을 반환하여 HTTP 응답에 로그인합니다.

특정 예로는 version_headerproject_header을 서명 할 문자열에 추가하는 것처럼 보입니다. 이들은 CanonicalHeaders이나 CanonicalExtensionHeaders의 목록에 없으므로 서버와 다른 문자열에 서명하고 있습니다.

여기에서 목록을 검토 할 수 있습니다. https://developers.google.com/storage/docs/reference/v1/developer-guidev1#authentication

관련 문제