使用JIRA API接收项目列表

时间:2015-08-05 14:54:35

标签: ios swift rest jira-rest-api

我想为iOS制作简单的JIRA助手,但无法弄清楚为什么API调用返回空数组。

例如,使用此代码我正在尝试获取项目列表:

let request = NSMutableURLRequest(URL: "http://myjira.atlassian.net/rest/api/2/project".URL!)
    request.addValue("Basic " + emailAndPasswordAsb64String, forHTTPHeaderField: "Authorization")
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.HTTPMethod = "GET"

let session = NSURLSession.sharedSession()

session.dataTaskWithRequest(request) { (data, response, error) -> Void in
    guard let data = data, responseString = NSString(data: data, encoding: NSUTF8StringEncoding) as? String where !responseString.isEmpty else {
        print("No valid response!", appendNewline: true)
        return
    }

    print("Response: " + responseString, appendNewline: true)

}.resume()

但得到这个:Response: []

我也尝试使用其他一些API,例如api/2/dashboard,但收到了这个: Response: {"startAt":0,"maxResults":20,"total":0,"dashboards":[]}

使用api/2/myself我收到了这个: Response: <?xml version="1.0" encoding="UTF-8" standalone="yes"?><status><status-code>401</status-code><message>Client must be authenticated to access this resource.</message></status>

也许我错过了什么,项目可以隐身?但是对于基本授权,我使用的是管理员登录名和密码。

1 个答案:

答案 0 :(得分:0)

终于明白了!

我犯了两个错误:

  1. http错了,https是对的!
  2. emailAndPasswordAsb64String是使用电子邮件和密码创建的,这是错误的!用户应输入username而不是email!对于管理员帐户,默认情况下用户名为admin
  3. 希望这会对某人有所帮助!