SugarSync API获取用户信息:未经授权的错误401

时间:2013-01-24 07:49:38

标签: api winhttp

我正在尝试使用WinHttp函数获取用户信息的请求。 SugarSync的REST API调用是referenced here

我的代码如下:

DWORD dwBytesWritten = 0;
DWORD dwStatusCode = 0;
DWORD dwSize = sizeof(DWORD);
BOOL  bResults = FALSE;
string data;
HINTERNET hSession = NULL, 
hConnect = NULL,
hRequest = NULL;

WINHTTP_PROXY_INFO proxyInfo;
winhttpintObj.setProxyInfo(proxyInfo);

HINTERNET hSession = WinHttpOpen(  L"A WinHTTP Example Program/1.0", 
                                   WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                                   WINHTTP_NO_PROXY_NAME, 
                                   WINHTTP_NO_PROXY_BYPASS, 0);

HINTERNET hConnect = WinHttpConnect( hSession, L"api.sugarsync.com",   
                                     INTERNET_DEFAULT_HTTPS_PORT, 0);

HINTERNET hRequest = WinHttpOpenRequest( hConnect, 
                                         L"GET", L"/user/<userID>", 
                                         NULL, 
                                         WINHTTP_NO_REFERER, 
                                         0, 
                                         WINHTTP_FLAG_SECURE);

string header("Authorization: ");
header += (accessToken);
header += "\n";
wstring ws = wstring(header.begin(), header.end());
WinHttpAddRequestHeaders( hRequest, ws.c_str(), (ULONG)-1L, WINHTTP_ADDREQ_FLAG_ADD );

WinHttpSendRequest( hRequest,
                    WINHTTP_NO_ADDITIONAL_HEADERS,
                    0,
                    WINHTTP_NO_REQUEST_DATA,
                    0, 
                    0, 
                    0 );

WinHttpReceiveResponse( hRequest, NULL);

WinHttpQueryHeaders( hRequest, 
                     WINHTTP_QUERY_STATUS_CODE |
                     WINHTTP_QUERY_FLAG_NUMBER,
                     NULL, 
                     &dwStatusCode, 
                     &dwSize, 
                     NULL );

cout << "dwStatusCode: " << dwStatusCode << endl;

accessTokenhttps://api.sugarsync.com/authorization/......。返回的状态代码为401: Authorization required。但是,我已经在标题中指定了它。我可以知道问题出在哪里以及解决问题的可能方法是什么?谢谢。

0 个答案:

没有答案