React-Native、Google Photos API 图像上传

标签 react-native base64 blob google-photos octetstring

react 原生+博览会

TL;DR:有没有人有一个使用应用程序内相机并将图像上传到"new"的react-native/expo的工作示例google api

  1. 验证用户(工作)
  2. Make Album (工作中)
  3. Make Album Shareable (工作中)
  4. 拍照(工作中)
  5. Get UploadToken (看起来它正在工作)
  6. Upload Photo (不工作)

为什么我认为(5)似乎有效,因为 getUploadToken 函数成功返回,响应 200,并提供了 key 。

为什么我认为(5)的另一端它可能是一个愚蠢的服务,我可以向它发布任何内容,它会成功返回。

我的预感是我将图像上传到/uploads 端点的方式有问题。

IE:格式不正确。

this.state.image == {'base64':'base64string','uri':'file://...',...}

我确实看到我的 Google 照片中正在创建一个相册,并且我可以看到它已设置为可共享,没有任何权限(用户无法评论或添加自己的照片)

2制作专辑

makeAlbum = () => {
    //MAKE ALBUM
    fetch('https://photoslibrary.googleapis.com/v1/albums', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer '+this.state.accessToken
      },
      body: JSON.stringify({
        "album": {"title": this.state.albumTemp}
      }),
    }).then((response) => response.json())
      .then((response) => {
        [
          this.shareAlbum(response),
          console.log('make: ',response)
        ]
    });
  }
}

3 使相册可共享

shareAlbum = (response) =>{
    this.setState({albumId:response.id})
    //MAKE ALBUM SHAREABLE
    fetch('https://photoslibrary.googleapis.com/v1/albums/'+this.state.albumId+':share',{
      method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': 'Bearer '+this.state.accessToken
        }
    }).then((response) => response.json())
      .then((response)=>{
        [
          this.setState({shareable:response.shareInfo.shareableUrl}),
          console.log('share1: ',this.state.shareable),
          console.log('share2: ',response),
        ]
    });
  }

4 拍摄照片

capturePhoto = async () => {
    let image = await this._camera.takePictureAsync({
      base64: true,
      quality: .5,
      width: 1920,
      fixOrientation: true
    })
    this.setState({ image: image, capturing: false })
    // delay the capture a few seconds to make sure it's rendered
    setTimeout(async () => {
      let result = await takeSnapshotAsync(this._previewRef, {
        format: 'png',
        result: 'tmpfile',
        quality: 1
      });
      //upload the photo to google photos album
      this.getUploadToken(image)
    }, 1000)
  }

5 获取上传 token

getUploadToken = (image) => {
    let name = this.state.image.uri.split('/')
    name=name[name.length-1]
    fetch('https://photoslibrary.googleapis.com/v1/uploads', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/octet-stream',
        'Authorization': 'Bearer '+this.state.accessToken,
        'X-Goog-Upload-File-Name': name,
        'X-Goog-Upload-Protocol': 'raw'
      },
      body:image,
    })
    .then((response) => {
      console.log('setup upload: ',response)
      this.uploadPhoto(response._bodyText); 
    })
  }

6 上传照片

uploadPhoto = (token) => {
fetch('https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer '+this.state.accessToken,
      },
      body:JSON.stringify({
        "albumId": this.state.albumId,
        "newMediaItems": [
          {
            "description": "Event Photo",
            "simpleMediaItem": {
              "uploadToken": token
            }
          }
        ]
      })
    })
    .then((response) => {
      console.log('actual upload: ',response)
      this.setState({ ready: true, image: null })
    })
  }

最佳答案

5 GET UPLOAD TOKEN API 可以正常工作,只是 Google 文档上的描述有误。输入不是 Base64,而是二进制形式。我在 Postman 中尝试过(下面的屏幕截图):

获取上传 token API: Get Upload Token API

上传媒体: enter image description here

关于React-Native、Google Photos API 图像上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53799216/

相关文章:

javascript - 将日期时间从一个偏移量转换为另一个偏移量

javascript - React Native UI 组件 : RCTBubblingEventBlock/RCTDirectEventBlock do not seem to work

javascript - Base64 图像源无法在 Cordova IOS 上运行

java - 在 JAVA 中使用 BLOB 获得更好的性能

php - 需要从mysql数据库显示图片到fpdf

android - 标题的值无法从 ReadablenativeMap 转换为字符串

javascript - 使用 validate.js 验证我的 TextInput 结果未定义

python - 如何通过 python 将 base64 图像嵌入到 SendGrid 电子邮件中?

javascript - 使用 JavaScript 创建 base64 编码的图像

javascript - 如何将音频Blob发布到服务器JavaScript和PHP