Vimeo API专辑或视频列表返回200但没有列表。 (原生反应)

时间:2018-04-24 00:30:08

标签: javascript reactjs fetch vimeo vimeo-api

我的目标是从我的Vimeo帐户中获取Vimeo中的相册列表或视频列表,其中所有内容都是私密的。我正在使用React Native和javascript fetch函数......

fetch(`https://api.vimeo.com/users/13477675/albums?client_id=<CLIENT_ID>`,{
            method: 'GET',
            page: 1,
            headers: new Headers({
                'Content-Type': 'application/vnd.vimeo.*+json',
                "Authorization" : "Bearer <TEST_TOKEN>"
            })
        }).then((response)=>{
            console.log('RESPONSE: '+JSON.stringify(response));
        });

我收到状态代码为200的JSON响应,就像它想象的那样。但是,我没有获得专辑或视频列表或任何类型的列表。 (当我使用Vimeo游乐场时,我会按预期获得相册或视频列表,并在那里使用相同的信息......)

以下是我的回应:

{"type":"default","status":200,"ok":true,"statusText":200,"headers":{"map":{"x-ratelimit-remaining":["95"],"x-ratelimit-limit":["100"],"expires":["Fri, 21 Apr 2028 00:26:23 GMT"],"connection":["keep-alive"],"cache-control":["no-cache, max-age=315360000"],"vary":["Accept,Vimeo-Client-Id,Accept-Encoding"],"content-type":["application/vnd.vimeo.album+json"],"date":["Tue, 24 Apr 2018 00:26:23 GMT"],"content-encoding":["gzip"],"via":["1.1 varnish, 1.1 varnish"],"x-cache-hits":["0, 0"],"accept-ranges":["bytes"],"strict-transport-security":["max-age=15552000; includeSubDomains; preload"],"x-cache":["MISS, MISS"],"x-ratelimit-reset":["2018-04-24T00:41:23+00:00"],"x-served-by":["cache-iad2143-IAD, cache-dca17724-DCA"],"age":["0"],"server":["nginx"],"x-timer":["S1524529583.137340,VS0,VE316"],"content-length":["3391"]}},"url":"","_bodyInit":{"listeners":{},"isRNFetchBlobPolyfill":true,"multipartBoundary":null,"_ref":"/Users/jeremyfrancis/Library/Developer/CoreSimulator/Devices/E1D3C5D3-5FF7-4C3A-81A4-B6A823F8A7AF/data/Containers/Data/Application/8AA6BEA8-EE15-4E6E-8AE2-A31CC571E19E/Documents/RNFetchBlob-blobs/blob-tb4fwue6s2ls7tjhcl8b","_blobCreated":true,"_closed":false,"cacheName":"blob-tb4fwue6s2ls7tjhcl8b","type":"text/plain","size":86998},"_bodyBlob":{"listeners":{},"isRNFetchBlobPolyfill":true,"multipartBoundary":null,"_ref":"/Users/jeremyfrancis/Library/Developer/CoreSimulator/Devices/E1D3C5D3-5FF7-4C3A-81A4-B6A823F8A7AF/data/Containers/Data/Application/8AA6BEA8-EE15-4E6E-8AE2-A31CC571E19E/Documents/RNFetchBlob-blobs/blob-tb4fwue6s2ls7tjhcl8b","_blobCreated":true,"_closed":false,"cacheName":"blob-tb4fwue6s2ls7tjhcl8b","type":"text/plain","size":86998}}

以下是我在寻找Vimeo Dev API的地方:https://developer.vimeo.com/api/reference/albums

更新

我已将我的代码更新为使用.json(),如下所示......

fetch(`https://api.vimeo.com/users/13477675/albums?client_id=<CLIENT_ID>`,{
        method: 'GET',
        page: 1,
        headers: new Headers({
            'Content-Type': 'application/vnd.vimeo.*+json',
            "Authorization" : "Bearer <TEST_TOKEN>"
        })
    }).then(response => response.json()).then((response)=>{
        console.log('RESPONSE: '+JSON.stringify(response));
    }).catch((error)=>{console.log(error.message});

这会捕获错误并打印:“无法解析blob的数据:( null)”

2 个答案:

答案 0 :(得分:1)

fetch调用的返回值是Response个对象。它不是你想要的实际对象 - 它是一个,在它被使用之前必须被解析成一个对象(或文本或其他)。

由于您正在处理JSON,因此您需要指示响应将JSON解析为对象:

fetch(`https://api.vimeo.com/users/13477675/albums?client_id=<CLIENT_ID>`, {
  method: 'GET',
  page: 1,
  headers: new Headers({
    'Content-Type': 'application/vnd.vimeo.*+json',
    "Authorization": "Bearer <TEST_TOKEN>"
  })
})
  .then(response => response.json())
  .then(responseObject => {
    console.log('RESPONSE: ' + JSON.stringify(responseObject));
    console.log(responseObject.type);
  });

除了检查其ok属性之外,在解析它之前,可以使用Response对象做很多有用的事情。

答案 1 :(得分:1)

我解决了我的问题。我意识到React Native不能很好地处理这样的事情但是react-native-fetch-blob npm包解决了这个问题。这是我的新工作代码:

{"f1": "value1"}
org.apache.kafka.common.errors.SerializationException: Error registering Avro schema: {"type":"record","name":"myrecord","fields":[{"name":"f1","type":"string"}]}
Caused by: io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException: Unexpected character ('<' (code 60)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: (sun.net.www.protocol.http.HttpURLConnection$HttpInputStream); line: 1, column: 2]; error code: 50005
    at io.confluent.kafka.schemaregistry.client.rest.RestService.sendHttpRequest(RestService.java:191)
    at io.confluent.kafka.schemaregistry.client.rest.RestService.httpRequest(RestService.java:218)
    at io.confluent.kafka.schemaregistry.client.rest.RestService.registerSchema(RestService.java:307)
    at io.confluent.kafka.schemaregistry.client.rest.RestService.registerSchema(RestService.java:299)
    at io.confluent.kafka.schemaregistry.client.rest.RestService.registerSchema(RestService.java:294)
    at io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient.registerAndGetId(CachedSchemaRegistryClient.java:61)
    at io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient.register(CachedSchemaRegistryClient.java:100)
    at io.confluent.kafka.serializers.AbstractKafkaAvroSerializer.serializeImpl(AbstractKafkaAvroSerializer.java:79)
    at io.confluent.kafka.formatter.AvroMessageReader.readMessage(AvroMessageReader.java:166)
    at kafka.tools.ConsoleProducer$.main(ConsoleProducer.scala:59)
    at kafka.tools.ConsoleProducer.main(ConsoleProducer.scala)

就是这样!我得到了正确的JSON返回。