簡體   English   中英

無法將GET請求發送到Twitter API,但可以發送POST請求嗎?

[英]Unable to send GET request to Twitter API, but can send POST request?

我們使用僅應用程序身份驗證,並獲取訪問令牌。 然后,我們通過執行以下操作向特定用戶的時間軸發出GET請求:

_bpos = snprintf(_buffer, sizeof(_buffer)-1, "GET /1.1/statuses/user_timeline.json?user_id=dog_rates&count=1 HTTP/1.1\n" //This is the HTTP request that gets no reply at all, even though it does send
                                                 "Host: api.twitter.com\n"
                                                 "Authorization: Bearer %s",authToken);

但這以某種方式起作用:

_bpos = snprintf(_buffer, sizeof(_buffer) - 1, "POST /oauth2/token HTTP/1.1\n"  //I used this to test to make sure the following read/write works, it did for me
                                                      "Host: api.twitter.com\n"
                                                      "Authorization: Basic 12312312312345678901234567890\n"
                                                      "Content-Type: application/x-www-form-urlencoded;charset=UTF-8\n"
                                                      "Content-Length: 29\n\n"
                                                      "grant_type=client_credentials");

因此,似乎只有GET類型的請求很麻煩。 我任何幫助將不勝感激!

_bpos = snprintf(_buffer, sizeof(_buffer)-1, "GET /1.1/statuses/user_timeline.json?user_id=dog_rates&count=1 HTTP/1.1\n" //This is the HTTP request that gets no reply at all, even though it does send
                                                 "Host: api.twitter.com\n"
                                                 "Authorization: Bearer %s",authToken);

    mbedtls_printf("Buffer content:\n%s\n",_buffer);   //prints the content of the buffer to ensure the HTTP request is right

    /* Sending REST API GET request */
    ret = mbedtls_ssl_write(&_ssl, (const unsigned char *) _buffer, _bpos); //writes the data to the socket
    mbedtls_printf("how far u gonna ret: %d\r\n",ret); //prints the return value of the write, positive is number of bytes written (should be equal to _bpos), negative is a failure
    if (ret < 0) //Checks to see if write failed
    {
        if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) //If write failed and didnt give back a read/write error, the TLS connection is broken, and the program prints an error
        {
            print_mbedtls_error("mbedtls_ssl_write", ret);
            onError(_tcpsocket, -1 );
        }
        return -1;
    }


     /* Read data out of the socket */
    usedspace = 0;
    bufptr = (unsigned char *) _buffer;//this and the above line are used to concatenate multiple reads into one buffer, however second read is commented out for now.

    ret = mbedtls_ssl_read(&_ssl, bufptr, static_cast<size_t>(RECV_BUFFER_SIZE-usedspace)); //read from socket into buffer
    mbedtls_printf("how far u gonna ret: %d\r\n",ret);//prints return value of read, same deal as write return value
    if(ret < 0)//same check that write does
    {
        if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
        {
            print_mbedtls_error("mbedtls_ssl_read", ret);
            onError(_tcpsocket, -1 );
        }
        delete[] buf;
        return -1;
    }
    mbedtls_printf("read finished\r\n");//placeholder text to see if you get this far

    usedspace = usedspace+ret;

    bufptr = bufptr + ret;

您的HTTP請求無效。 您在GET請求中的最后一個標頭之后缺少換行符,並且用\\n分隔標頭行,而不用\\r\\n分隔標頭行(另請參閱此內容 )。

我建議您使用mbed-http之類的庫來為您建立請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM