在google的文档“Using OAuth 2.0 for Devices”中,提到我们应该在控制台中注册应用程序后将以下信息发送到accounts.google.com。
POST /o/oauth2/device/code HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
client_id=812741506391-h38jh0j4fv0ce1krdkiq0hfvt6n5amrf.apps.googleusercontent.com& scope=email%20profile
i implement that in c with the follwing steps:
1:声明一个全局数组:
static char gmail_request_token_url[] = "POST /o/oauth2/device/code HTTP/1.1\r\n"
"Host: accounts.google.com\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: 289\r\n"
"Accept: text/html,*/*\r\n"
"User-Agent: Mozilla/5.0\r\n"
"client_id=my.apps.googleusercontent.com&scope=email%20profile\r\n";
2:创建连接到“accounts.google.com”端口(80 或 443)的套接字并使用发送:
if(send(socket_fd, gmail_get_tokens, sizeof(gmail_get_tokens),0) > 0)
{
printf("successfully send content to google");
}
3:使用 while 循环来接收服务器的响应
while(1)
{
ret = recv(......);
}
不幸的是,recv 的返回值始终为 0。这意味着它没有收到任何内容。我不知道出了什么问题。请帮助我。
最佳答案
the code i wrote is as follow.please so kind to help me find the problem in it.
/* File Name: server.c */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <unistd.h>
#define DEFAULT_PORT 8002
#define MAXLINE 4096
#define GMAIL_ACCOUNTS_SERVER "accounts.google.com"
static char gmail_request_token_url[] = "POST /o/oauth2/device/code HTTP/1.1\r\n"
"Host: accounts.google.com\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: 289\r\n"
"Accept: text/html,*/*\r\n"
"User-Agent: Mozilla/5.0\r\n"
"client_id=my.apps.googleusercontent.com&scope=email%20profile\r\n";
static char gmail_get_tokens[] ="POST /oauth2/v3/token HTTP/1.1\r\n"
"Host: accounts.google.com\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: 389\r\n"
"Accept: text/html,*/*\r\n"
"User-Agent: Mozilla/5.0\r\n"
"client_id=my.apps.googleusercontent.com&client_secret={my_client_secret}&code=authentication_code&grant_type=http://oauth.net/grant_type/device/1.0\r\n";
void main(void)
{
int socket_fd = -1;
struct sockaddr_in servaddr;
struct sockaddr_in clientaddr;
char buff[4096];
int n;
struct hostent *host = NULL;
int reuse = 1;
struct timeval tv;
fd_set rset;
int ret = 0;
char requestbuffer[1024]={0};
// printf("the sizeof the request array is %d\n",sizeof(gmail_request_token_url));
if( (socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){
printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
memset(buff,0,sizeof(buff));
memset(&servaddr, 0, sizeof(servaddr));
memset(&clientaddr,0,sizeof(clientaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(DEFAULT_PORT);
clientaddr.sin_family = AF_INET;
clientaddr.sin_port = htons(443);
if (setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(int)) < 0)
{
printf("fail to set socket reuse\n");
}
else
{
printf("successfully set sock reuse\n");
}
if( bind(socket_fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){
printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
exit(0);
}
host = gethostbyname(GMAIL_ACCOUNTS_SERVER);//"get ip address of accounts.google.com"
if(host != NULL)
{
printf("successfully get google account name\n");
clientaddr.sin_addr.s_addr = *(unsigned long *)host->h_addr_list[0];
printf("the address is %s\n",inet_ntoa(clientaddr.sin_addr));
}
else
{
printf("fail to get google account name\n");
clientaddr.sin_addr.s_addr = inet_addr("216.58.217.205");
}
if( connect(socket_fd, (struct sockaddr*)&clientaddr, sizeof(clientaddr)) < 0)
{
printf("connect error: %s(errno: %d)\n",strerror(errno),errno);
return;
}
else
{
printf("successfull connect to the server\n");
}
// if(send(socket_fd, gmail_request_token_url, sizeof(gmail_request_token_url),0) == -1)
if(send(socket_fd, gmail_get_tokens, sizeof(gmail_get_tokens),0) == -1)
{
printf("fail to send content to google");
// continue;
}
else
{
printf("successfulyy send request url %s\n",gmail_get_tokens);
}
FD_ZERO(&rset);
FD_SET(socket_fd,&rset);
while(1)
{
tv.tv_sec = 5;
tv.tv_usec = 0;
ret = select(socket_fd+1,&rset,NULL,NULL,&tv);
printf("the ret is %d\n",ret);
if (ret > 0)
{
printf("waiting for response from server\n");
if (FD_ISSET(socket_fd,&rset))
{
printf("it is the fd,response");
n = recv(socket_fd, buff, 1024, 0);
if (n > 0)
{
printf("successfully recv content from the server\n");
buff[n] = '\0';
printf("recv msg from client: %s %s\n", buff,strerror(errno));
}
else
{
printf("fail to recv any message from the server %s\n",strerror(errno));
//close(fd);
//break;
}
}
}
else if(0 == ret)
{
printf("time out\n");
continue;
}
else
{
printf("the ret is less than 0\n");
continue;
}
sleep(1);
}
close(socket_fd);
// return 0;
}
关于c - 如何用c语言实现gmail的Oauth2.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063120/