2015-12-07 35 views
0

我寫了一個應用程序來保存對象存儲中的文件。 我在連接到對象存儲,當Bluemix連接返回一個錯誤的問題({的AuthenticationException消息=未經授權,狀態= 401})對象存儲返回未授權的Java連接401

我使用的openstack4j實現,這是我的代碼:

String envServices = System.getenv("VCAP_SERVICES"); 
    if (envServices != null) { 
     JSONParser parser = new JSONParser(); 
     Object obj = parser.parse(envServices); 
     JSONObject jsonObject = (JSONObject) obj; 
     JSONArray vcapArray = (JSONArray) jsonObject.get("Object-Storage"); 
     JSONObject vcap = (JSONObject) vcapArray.get(0); 
     JSONObject credentials = (JSONObject) vcap.get("credentials"); 
     username = credentials.get("username").toString(); 
     password = credentials.get("password").toString(); 
     auth_url = credentials.get("auth_url").toString() + "/v3"; 
     domain = credentials.get("domainId").toString(); 
     project = credentials.get("projectId").toString(); 
    } else { 
     username = "someuser"; 
     password = "somepassword"; 
     auth_url = "https://identity.open.softlayer.com"; 
     domain = "sfsd"; 
     project = "object_storage_xxxxxxx"; 
    } 
    Identifier domainIdent = Identifier.byName(domain); 
    Identifier projectIdent = Identifier.byName(project); 


    OSClient os = OSFactory.builderV3().endpoint(auth_url).credentials(username, password,domainIdent).scopeToProject(projectIdent, domainIdent) 
      .authenticate(); 
    objectStorage = os.objectStorage(); 
    account = objectStorage.account().get(); 

任何幫助將不勝感激。 阿西

回答

0

你VCAP_SERVICES是這樣的:

{ 
"Object-Storage": [ 
{ 
    "name": "Object-Storage - YP", 
    "label": "Object-Storage", 
    "plan": "Free", 
    "credentials": { 
    "auth_url": "https://identity.open.softlayer.com", 
    "project": "object_storage_d049255b", 
    "projectId": "0f47b41b06d047f9aae3b33f1db061ed", 
    "region": "dallas", 
    "userId": "ad78b2a3f843466988afd077731c61fc", 
    "username": "user_202db1f8a7aa3f3ac51ec68f10dbe7dc29070bc7", 
    "password": "K/jyIi2jR=1?D.TP", 
    "domainId": "2df6373c549e49f8973fb6d22ab18c1a", 
    "domainName": "639347" 
    } 
    } 
    ] 
} 

所以,你必須要改變這種行:

domain = credentials.get("domainId").toString(); 
project = credentials.get("projectId").toString(); 

有:

domain = credentials.get("domainName").toString(); 
project = credentials.get("project").toString(); 
0

這段代碼將有助於您將Object Storage服務連接到您的應用程序

String envServices = System.getenv("VCAP_SERVICES"); 
JSONParser parser = new JSONParser(); 

try{ 

Object obj = parser.parse(envServices); 
JSONObject jsonObject = (JSONObject) obj; 
JSONArray vcapArray = (JSONArray) jsonObject.get("Object-Storage"); 
JSONObject vcap = (JSONObject) vcapArray.get(0); 
JSONObject credentials = (JSONObject) vcap.get("credentials"); 
String userId = credentials.get("userId").toString(); 
String password = credentials.get("password").toString(); 
String auth_url = credentials.get("auth_url").toString() + "/v3"; 
String domain = credentials.get("domainName").toString(); 
String project = credentials.get("project").toString(); 
Identifier domainIdent = Identifier.byName(domain); 
Identifier projectIdent = Identifier.byName(project); 

OSClient os = OSFactory.builderV3() 
       .endpoint(auth_url) 
       .credentials(userId, password) 
       .scopeToProject(projectIdent, domainIdent) 
       .authenticate(); 
SwiftAccount account = os.objectStorage().account().get(); 

}catch(Exception e){ 
    e.printStackTrace(); 
} 
0

問題是此行>>Identifier domainIdent = Identifier.byName(domain);

它應該是這樣的>>Identifier domainIdent = Identifier.byId(domain); 因爲你逝去的域ID不是域名。