#include "stdafx.h"
#include <Windows.h>
#include <WinInet.h>
#include <tchar.h>
#include <strsafe.h>
#include <string>
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);
void LogOut();
int _tmain(int argc, _TCHAR* argv[])
{
ClientLogin(L"",L"");
printf(g_szAuth);
printf("/n");
LogOut();
return 0;
}
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;
}
}
return bLoginSuceess;
}
void LogOut()
{
InternetCloseHandle(g_hRequest);
InternetCloseHandle(g_hConnect);
InternetCloseHandle(g_hSession);
}