获取访问令牌时出错 "HTTP method POST is not supported by this URL, StatusCode=405"

Getting error for getting access token "HTTP method POST is not supported by this URL, StatusCode=405"

使用触发器和顶点获取访问令牌时遇到问题 class。我正在使用“https://www.googleapis.com/auth/drive”作为回调 URL 和 HTTP 请求的端点。如果提供了有效的访问令牌,我的创建文件夹方法可以正常工作,但我没有获得访问令牌。但我收到错误消息“此 URL 不支持 HTTP 方法 POST,StatusCode=405”

下面是我的代码

public class GDriveFolderCreationClass {
private final String clientId ='3MVG98EE59.VIHmz7DO7_********************kb0NbJrDULh.q0CmS3TqSuItCtA6mxyxUaa_STYbpue';
private final String clientSecret = '8E70141F********************6307D13F5B72FD850ABA2C9A05124F3B7B9F';
private final String username = 'test@gmail.com';
public class deserializeResponse{
 public String access_token;
}
public String ReturnAccessToken (GDriveFolderCreationClass acount){
deserializeResponse resp1= new deserializeResponse();
String reqbody = 'client_id='+clientId+'&client_secret='+clientSecret+'&username='+username;
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setBody(reqbody);
req.setMethod('POST');
req.setEndpoint('https://www.googleapis.com/auth/drive');
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept','application/json');
HttpResponse res = h.send(req);
if(res.getstatusCode() == 200 && res.getbody() != null){
    resp1 = (deserializeResponse)JSON.deserialize(res.getbody(),deserializeResponse.class);
}
return resp1.access_token;
}

@future(Callout=True)
public static void createFolderinDrive(String contentName){ 
GDriveFolderCreationClass account1 = new GDriveFolderCreationClass();
String accessToken;
accessToken = account1.ReturnAccessToken(account1);
createFolder();
}
//Working function for creating folder in google drive
public static void createFolder() {
HttpRequest req = new HttpRequest();
req.setMethod('POST');     
req.setEndpoint('https://www.googleapis.com/drive/v3/files');
req.setHeader('Authorization', 'Bearer '+accessToken);
req.setHeader('content-type', 'application/json');

String body = '{"name" : "'+'TestFolder'+'","mimeType" : "application/vnd.google-apps.folder"}';
req.setTimeout(60*1000);
req.setBody(body);

Http http = new Http();
HttpResponse res = http.send(req);
}
}

ConnectedAppSS 我还使用了 AUTH 提供程序并使用回调 URL 作为重定向 URI,但这也没有用。为此,我在调试日志中遇到以下错误 error ss 请帮我获取固定 google 帐户的访问令牌,以便在我的 google 驱动器中创建文件夹结构。如果您需要任何其他详细信息,请告诉我。

感谢和问候

您正在使用 https://www.googleapis.com/auth/drive 作为 POST 您请求令牌的端点。此 URL 没有 return 任何授权令牌。

参见https://developers.google.com/identity/protocols/oauth2#2.-obtain-an-access-token-from-the-google-authorization-server

获取授权令牌的端点;使用客户端库更容易做到的是:https://accounts.google.com/o/oauth2/v2/auth

首先使用代码授权获取刷新令牌,然后您可以使用刷新令牌获取访问令牌。 使用 "https://accounts.google.com/o/oauth2/token" 作为端点,通过使用刷新令牌一次又一次地获取访问令牌。