我们使用仅应用程序身份验证并获取访问令牌。然后,我们通过执行以下操作向特定用户的时间表发出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;