下面代码是我写来测试的,用户名和密码可以成功通过验证了,但是要获取上传视频的token的时候总是返回验证无效的代码401。
欢迎大家加入研究讨论,有关详细信息可以参考youtube api。#include "stdafx.h"
#include <Windows.h>
#include <WinInet.h>
#include <tchar.h>
#include <strsafe.h>
#include <string>
#include "File.h"
using namespace std;
#pragma comment( lib, "Wininet.lib" ) LPCTSTR lpszAcceptType = L"Accept: text/*";
CHAR g_szAuth[1024];
TCHAR g_szYouTubeUser[128];HINTERNET g_hSession = NULL;
HINTERNET g_hConnect = NULL;
HINTERNET g_hRequest = NULL;
BOOL ClientLogin(LPTSTR lpUser,LPTSTR lpPassword);
BOOL GetUploadToken();
void LogOut();int _tmain(int argc, _TCHAR* argv[])
{
DWORD err = -1;
err = ClientLogin(L"***@gmail.com",L"*****");
printf(g_szAuth);
printf("\n");
GetUploadToken();
LogOut(); return 0;
}
BOOL GetUploadToken()
{
BOOL bSuccess = FALSE;
LPTSTR szObjectName = L"/action/GetUploadToken"; g_hRequest = HttpOpenRequest(g_hConnect,L"POST",szObjectName,NULL,NULL,&lpszAcceptType,INTERNET_FLAG_RELOAD,0);
if (!g_hRequest)
{
return bSuccess;
}
File file;
TCHAR *pXmlData=NULL;
DWORD dwXmlDataSize=0;
if(file.Open(L"..\\UploadFile.xml",FILE_READ_DATA))
{
pXmlData = new TCHAR(file.GetLength());
dwXmlDataSize = file.Read(pXmlData,file.GetLength());
} TCHAR lpHeader[1024];;
LPTSTR lpHeaderFotmat = L"Host: gdata.youtube.com\r\nAuthorization: GoogleLogin %s\r\nX-GData-Client: ytapi-wondershare-MovieStory-99fhq3ri-0\r\nX-GData-Key: key=AI39si7Y5dh4_Exis_G-GbIIC2RjmJHSV3id5oi2_WBt5qLY2ut3ceX3e3vJzePtwFj6_pLcIQTkN347cNHbYcB_kwYl2IX-qQ\r\nContent-Length: %d\r\nContent-Type: application/atom+xml; charset=UTF-8\r\n";
TCHAR szAuth[1024];
MultiByteToWideChar( CP_ACP, 0, g_szAuth,
strlen(g_szAuth)+1, szAuth,   
sizeof(szAuth)/sizeof(szAuth[0]) );
DWORD len = lstrlen(lpHeaderFotmat)+lstrlen(szAuth);
//lpHeader = new TCHAR(len);
StringCchPrintf(lpHeader,len,lpHeaderFotmat,szAuth,dwXmlDataSize);
BOOL bSendRequest = HttpSendRequest(g_hRequest,lpHeader,lstrlen(lpHeader),pXmlData,dwXmlDataSize );
if (!bSendRequest)
{
return bSuccess;
}
CHAR code[1024];
DWORD size = 1024;
HttpQueryInfoA(g_hRequest,HTTP_QUERY_STATUS_CODE ,code,&size,0); printf(code);printf("\n");
return bSuccess;
}
BOOL ClientLogin (LPTSTR lpUser,LPTSTR lpPassword)
{
BOOL bLoginSuceess = FALSE;


g_hSession = InternetOpen(NULL,INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!g_hSession)
{
return bLoginSuceess;
} LPTSTR lpszHost = L"www.google.com";
g_hConnect = InternetConnect(g_hSession, lpszHost,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (!g_hConnect)
{
return bLoginSuceess;
} LPTSTR lpszObjectNameFormat = L"/accounts/ClientLogin?AccountType=HOSTED_OR_GOOGLE&Email=%s&Passwd=%s&&service=xapi&source=Wondershare-MovieStroy-v4.5.0";
TCHAR szObjectName[1024];
memset(szObjectName,0,sizeof(szObjectName));
StringCchPrintf(szObjectName,1024,lpszObjectNameFormat,lpUser,lpPassword); g_hRequest = HttpOpenRequest(g_hConnect,L"POST",szObjectName,NULL,NULL,&lpszAcceptType,INTERNET_FLAG_RELOAD,0);
if (!g_hRequest)
{
return bLoginSuceess;
}

const TCHAR szHeaders[] =L"Content-Type:application/x-www-form-urlencoded"; 
BOOL bSendRequest = HttpSendRequest(g_hRequest,szHeaders,lstrlen(szHeaders),NULL,0 );
if (!bSendRequest)
{
return bLoginSuceess;
}
DWORD  dwSize;
LPSTR  lpszData;    // buffer for the data
DWORD  dwDownloaded; // size of the downloaded data
if (!InternetQueryDataAvailable(g_hRequest,&dwSize,0,0))
{
return bLoginSuceess;
}
else
{
lpszData = new CHAR[dwSize+1]; if(!InternetReadFile(g_hRequest,
(LPVOID)lpszData,
dwSize,
&dwDownloaded))
{
delete[] lpszData;
return bLoginSuceess;
}
else
{
// Add a null terminator to the end of the data buffer
lpszData[dwDownloaded]='\0'; string strData = lpszData;
int index = strData.find("Auth");
bLoginSuceess = index!=-1 ? TRUE:FALSE;
memset(g_szAuth,0,sizeof(g_szAuth));
CopyMemory(g_szAuth,lpszData+index,dwDownloaded-(index+1));
g_szAuth[dwDownloaded-index-1]='\0';

delete[] lpszData;
} }
InternetCloseHandle(g_hRequest);
return bLoginSuceess;
}
void LogOut()
{

InternetCloseHandle(g_hConnect);
InternetCloseHandle(g_hSession);
}