Search code examples
djangotwittertwitter-oauth

Code 324 Not Valid Video and Status failed?


I am trying to upload video through twitter API from my website. I scraped their github library code's async upload file for large files. I am uploading the data in chunks. This is the code: (Note I am using static file size and chunks for the testing purpose would definitely appreciate a dynamic method suggestion)

    MEDIA_ENDPOINT_URL = 'https://upload.twitter.com/1.1/media/upload.json'
    POST_TWEET_URL = 'https://api.twitter.com/1.1/statuses/update.json'

    CONSUMER_KEY = 'xxx'
    CONSUMER_SECRET = 'xxx'
    ACCESS_TOKEN = 'xxx-xxx'
    ACCESS_TOKEN_SECRET = 'xxx'

    VIDEO_FILENAME = request.FILES['video']
    VIDEO_SIZE = 59467

    oauth = OAuth1(CONSUMER_KEY,
                   client_secret=CONSUMER_SECRET,
                   resource_owner_key=ACCESS_TOKEN,
                   resource_owner_secret=ACCESS_TOKEN_SECRET)

    request_data = {
      'command': 'INIT',
      'media_type': 'video/mp4',
      'total_bytes': VIDEO_SIZE,
      'media_category': 'tweet_video'
    }

    req = requests.post(url=MEDIA_ENDPOINT_URL, data=request_data, auth=oauth)
    print req
    media_id = req.json()['media_id']

    print('Media ID: %s' % str(media_id))

    segment_id = 0
    bytes_sent = 0
    vid_file = VIDEO_FILENAME

    while bytes_sent < VIDEO_SIZE:
        chunk = vid_file.read(59467)
        print('APPEND')

        request_data = {
            'command': 'APPEND',
            'media_id': media_id,
            'segment_index': segment_id
        }

        files = {
            'media': chunk
        }

        req = requests.post(url=MEDIA_ENDPOINT_URL, data=request_data, files=files, auth=oauth)

        if req.status_code < 200 or req.status_code > 299:
            print(req.status_code)
            print(req.text)

        segment_id = segment_id + 1
        bytes_sent = vid_file.tell()

        print('%s of %s bytes uploaded' % (str(bytes_sent), str(VIDEO_SIZE)))

    print('Upload chunks complete.')

    request_data = {
        'command': 'FINALIZE',
        'media_id': media_id,
        'media_category': 'tweet_video'
    }

    req = requests.post(url=MEDIA_ENDPOINT_URL, data=request_data, auth=oauth)
    print(req.json())

    processing_info = req.json().get('processing_info', None)
    print(req.status_code)

    time.sleep(5)

    request_data = {
        'status': 'I just uploaded a video with the @TwitterAPI.',
        'media_ids': req.json()['media_id_string'],
        'media_category': 'tweet_video'
    }

    req = requests.post(url=POST_TWEET_URL, data=request_data, auth=oauth)
    print(req.json())

    context = {
        'r': req
    }
    return render_to_response('dashboard/manage_content/display.html', context) 

I am hit by the following error:

{"errors":[{"code":324,"message":"Not valid video"}]} I am uploading a mp4 file of 1.6 mb size. Please let me know if you need any more info.


Solution

  • The solution was to use the actual size of the file instead of just a part of it and make sure you use a function to dynamically get the size. Static size does not work even if all the chunks get uploaded.