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

Gop*_*pan 6 angularjs google-cloud-datastore ionic-framework

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

$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.
  });
Run Code Online (Sandbox Code Playgroud)

角度请求中出错**

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 ()
Run Code Online (Sandbox Code Playgroud)

在邮差中尝试时出错

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"
      }
    }
Run Code Online (Sandbox Code Playgroud)

但我得到了Google Developer网站的回复

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"
   }
  ]
 }
}
Run Code Online (Sandbox Code Playgroud)

Tad*_*zys 0

我不是 angularjs 用户,但查看错误我猜你还没有完成身份验证。Google 正在使用 OAuth 2.0 进行身份验证,您可以在此处阅读相关信息

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