1

I'm kind of confused with the documentation. I did my research, little resources. Maybe someone can help me out. i'm using a CRNA - create-react-app. i have a button that suppose to upload a file using URI to Google Drive. I already have the initial steps like API key, client id, enable API like Drive api. I manage to make Google Picker work. but no luck with Google Drive upload.

There is a save-to-drive button embedded UI. just copy paste, works in plain HTML. But what i need is to create my own method to execute the upload process. Thank you all.

King Jherold
  • 129
  • 3
  • 14
  • Would be great if you could share some code. Even better if you can provide a working js fiddle or sandbox – Swapnil Jan 03 '19 at 05:26
  • as much as i want to share some working code. i don't have any, sorry. im just trying out some code from the documentation but it is in nodeJS and try to but can't make it work with react app. this is the [documentation link](https://developers.google.com/drive/api/v3/manage-uploads). @Swapnil – King Jherold Jan 03 '19 at 05:33
  • Have you checked this blogpost - [How to set up file upload with React and Node](https://medium.freecodecamp.org/how-to-create-file-upload-with-react-and-node-2aa3f9aab3f0)? – Jessica Rodriguez Jan 03 '19 at 09:39
  • @jess The link is not working. – Shubham Chadokar Nov 13 '19 at 13:35

1 Answers1

1

Please use following steps, I hope it will help:

  1. Add Package yarn add react-google-picker into your project, Package Link here
  2. Get your Google Developer Key, Client Id ready from https://console.developers.google.com
  3. Implement following code:

Note: Do not forget to change 'YOUR_DEVELOPER_KEY_HERE', 'YOUR_CLIENT_ID_HERE' with your keys in following code:

import React, {Component} from 'react';
import GooglePicker from 'react-google-picker';

class MyPage extends Component{
  render(){
   return (
      <GooglePicker clientId={'YOUR_CLIENT_ID_HERE'}
              developerKey={'YOUR_DEVELOPER_KEY_HERE'}
              scope={['https://www.googleapis.com/auth/drive.readonly']}
              onChange={data => console.log('on change:', data)}
              onAuthFailed={data => console.log('on auth failed:', data)}
              multiselect={true}
              navHidden={true}
              authImmediate={false}
              viewId={'DOCS'}
              mimeTypes={['image/png', 'image/jpeg', 'image/jpg']}
              createPicker={ (google, oauthToken) => {
                const googleViewId = google.picker.ViewId.DOCS;
                const uploadView = new google.picker.DocsUploadView();
                const docsView = new google.picker.DocsView(googleViewId)
                    .setIncludeFolders(true)
                    .setSelectFolderEnabled(true);

                const picker = new window.google.picker.PickerBuilder()
                .enableFeature(google.picker.Feature.SIMPLE_UPLOAD_ENABLED)
                  .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
                    .addView(docsView)
                    .addView(uploadView)/*DocsUploadView added*/
                    .setOAuthToken(oauthToken)
                    .setDeveloperKey('YOUR_DEVELOPER_KEY_HERE')
                    .setCallback((data)=>{
                      if (data.action == google.picker.Action.PICKED) {
                          var fileId = data.docs[0].id;
                          alert('The user selected: ' + fileId);
                          picker();
                      }
                    });
                picker.build().setVisible(true);
            }}>
            <span>Click here</span>
            <div className="google"></div>
        </GooglePicker>
     );
   }
}

export default MyPage

This is how it looks ;)

enter image description here

Mohit
  • 776
  • 7
  • 13
  • What is `YOUR_DEVELOPER_KEY_HERE` ? – gdfgdfg Jul 11 '19 at 21:31
  • 1
    @gdfgdfg its a key you will have to get from google developer API. – Mohit Jul 11 '19 at 21:34
  • There is only one API key, there, but here we have and client ID which is Oauth. I can't understand, need an API key and Oauth credentials ? – gdfgdfg Jul 11 '19 at 21:42
  • but this doesn't matter, because I am getting `on auth failed: TypeError: Cannot read property 'postMessage' of null` – gdfgdfg Jul 11 '19 at 21:55
  • Im getting an error on the `picker()` im assuming that this is bespoke code and not the picker or google code, just removing that bit seems as everything else works – Max Carroll Nov 01 '19 at 11:54
  • Weird. Im getting this error message when importing GooglePicker Module not found: Can't resolve 'react' in '...\node_modules\react-google-picker\dist' – chitgoks Nov 04 '19 at 04:49
  • is there any way to do this without picker? – SalahAdDin Jul 03 '20 at 16:28