如何使用Anguarjs $ Http POST请求从Google Data Store访问数据?

时间:2017-01-19 18:07:11

标签: angularjs ionic-framework google-cloud-datastore

我试过,下面是我的代码,我不知道出了什么问题

$http({
  method: 'POST',
  headers:'{"gqlQuery": {"queryString": "select * from culture" }}',
  url: 'https://datastore.googleapis.com/v1/projects/nammaooru-3630e:runQuery?fields=batch%2Cquery&key=[myapikey]'
}).then(function successCallback(response) {

  alert("Success"+response);
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    alert("Error"+response);
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

角度请求**出错

  

https://datastore.googleapis.com/v1/projects/nammaooru-3630e:runQuery?fields=batch%2Cquery&key=[apikey]

Failed to load resource: the server responded with a status of 401 ()

在Postman中尝试时出错

URL : https://datastore.googleapis.com/v1/projects/nammaooru-3630e:runQuery?fields=batch%2Cquery&key=[apikey]

Method : POST 


    {
      "error": {
        "code": 401,
        "message": "The request does not have valid authentication credentials.",
        "status": "UNAUTHENTICATED"
      }
    }

但是我得到了Google开发者网站的回复

https://cloud.google.com/datastore/docs/reference/rest/v1/projects/runQuery#http-request

{
 "batch": {
  "entityResultType": "FULL",
  "entityResults": [
   {
    "entity": {
     "key": {
      "partitionId": {
       "projectId": "nammaooru-3630e"
      },
      "path": [
       {
        "kind": "culture",
        "id": "5629499534213120"
       }
      ]
     },
     "properties": {
      "types": {
       "stringValue": "",
       "excludeFromIndexes": true
      },
      "images": {
       "stringValue": "",
       "excludeFromIndexes": true
      },
      "bname": {
       "stringValue": "Kangeyam Kalai"
      },
      "date": {
       "timestampValue": "2017-01-14T12:44:56.933Z"
      },
      "title": {
       "stringValue": ""
      },
      "description": {
       "stringValue": "Coimbatore Kongu nadu region",
       "excludeFromIndexes": true
      }
     }
    },
    "cursor": "Ci8SKWoRc35uYW1tYW9vcnUtMzYzMGVyFAsSB2N1bHR1cmUYgICAgICAgAoMGAAgAA==",
    "version": "1484398012256000"
   }
  ],
  "endCursor": "Ci8SKWoRc35uYW1tYW9vcnUtMzYzMGVyFAsSB2N1bHR1cmUYgICAgICAgAoMGAAgAA==",
  "moreResults": "NO_MORE_RESULTS"
 },
 "query": {
  "kind": [
   {
    "name": "culture"
   }
  ]
 }
}

3 个答案:

答案 0 :(得分:1)

- 禁用网络安全并通过在窗口中运行以下命令重新启动Chrome"运行":

chrome --allow-file-access-from-files --disable-web-security --user-data-dir

答案 1 :(得分:0)

我怀疑这是与CORS相关的问题。你有POSTMAN的访问权吗?

另一件事,来自标题的查询字符串应该在请求正文中。

答案 2 :(得分:0)

我不是一个angularjs用户,但看着错误,我猜你还没有完成身份验证。 Google正在使用OAuth 2.0进行身份验证,您可以阅读here

基本上,您需要请求身份验证令牌,然后在api调用中使用获取的令牌。 (那就是你缺少的标题位,授权:Bearer yourtokenhere