我试图建立一个连接器从谷歌云下载文件。为此,我使用云存储API客户端库Java(v1beta2-rev23-1.17.0-rc)
我犯了一个错误
400错误请求:“错误”:“未经授权的\u客户端”
无法下载文件。
代码是
https://gist.github.com/yashk/9226613
/**
* Main class for the Cloud Storage API command line sample.
* Demonstrates how to make an authenticated API call using OAuth 2 helper classes.
*/
public class StorageSample {
/**
* Be sure to specify the name of your application. If the application name is {@code null} or
* blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0".
*/
private static final String APPLICATION_NAME = "MyCompnay/1.0";
/** Directory to store user credentials. */
private static final java.io.File DATA_STORE_DIR =
new java.io.File(System.getProperty("user.home"), ".store/storage_sample");
/**
* Global instance of the {@link DataStoreFactory}. The best practice is to make it a single
* globally shared instance across your application.
*/
private static FileDataStoreFactory dataStoreFactory;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/** Global instance of the HTTP transport. */
private static HttpTransport httpTransport;
@SuppressWarnings("unused")
private static Storage client;
/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
// load client secrets
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(StorageSample.class.getResourceAsStream("/client_secrets.json")));
if (clientSecrets.getDetails().getClientId().startsWith("Enter") ||
clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
System.out.println(
"Overwrite the src/main/resources/client_secrets.json file with the client secrets file "
+ "you downloaded from the Quickstart tool or manually enter your Client ID and Secret "
+ "from https://code.google.com/apis/console/?api=storage#project:<project_id> "
+ "into src/main/resources/client_secrets.json");
System.exit(1);
}
// Set up authorization code flow.
// Ask for only the permissions you need. Asking for more permissions will
// reduce the number of users who finish the process for giving you access
// to their accounts. It will also increase the amount of effort you will
// have to spend explaining to users what you are doing with their data.
// Here we are listing all of the available scopes. You should remove scopes
// that you are not actually using.
Set<String> scopes = new HashSet<String>();
scopes.add(StorageScopes.DEVSTORAGE_FULL_CONTROL);
scopes.add(StorageScopes.DEVSTORAGE_READ_ONLY);
scopes.add(StorageScopes.DEVSTORAGE_READ_WRITE);
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets, scopes)
.setDataStoreFactory(dataStoreFactory)
.build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
public static void main(String[] args) {
try {
// initialize the transport
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
// initialize the data store factory
dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
// authorization
Credential credential = authorize();
// set up global Storage instance
client = new Storage.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
client.objects().list("<bucket_id>").executeAndDownloadTo(System.out);
} catch (IOException e) {
System.err.println(e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
System.exit(1);
}
}
}
错误日志为https://gist.github.com/yashk/9226613
Feb 26, 2014 9:28:05 AM com.google.api.client.http.HttpRequest execute
CONFIG: -------------- REQUEST --------------
POST https://accounts.google.com/o/oauth2/token
Accept-Encoding: gzip
User-Agent: Google-HTTP-Java-Client/1.17.0-rc (gzip)
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 208
Feb 26, 2014 9:28:05 AM com.google.api.client.http.HttpRequest execute
CONFIG: curl -v --compressed -X POST -H 'Accept-Encoding: gzip' -H 'User-Agent: Google-HTTP-Java-Client/1.17.0-rc (gzip)' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' -d '@-' -- 'https://accounts.google.com/o/oauth2/token' << $$$
Feb 26, 2014 9:28:05 AM com.google.api.client.util.LoggingByteArrayOutputStream close
CONFIG: Total: 208 bytes
Feb 26, 2014 9:28:05 AM com.google.api.client.util.LoggingByteArrayOutputStream close
CONFIG: grant_type=refresh_token&refresh_token=<removed_for_sec>&client_id=<removed_for_sec>&client_secret=<removed_for_sec>
Feb 26, 2014 9:28:05 AM com.google.api.client.http.HttpResponse <init>
CONFIG: -------------- RESPONSE --------------
HTTP/1.1 400 Bad Request
Expires: Fri, 01 Jan 1990 00:00:00 GMT
X-XSS-Protection: 1; mode=block
Alternate-Protocol: 443:quic
Server: GSE
X-Content-Type-Options: nosniff
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
Date: Wed, 26 Feb 2014 09:28:01 GMT
Transfer-Encoding: chunked
Content-Encoding: gzip
Content-Type: application/json
Feb 26, 2014 9:28:05 AM com.google.api.client.util.LoggingByteArrayOutputStream close
CONFIG: Total: 37 bytes
Feb 26, 2014 9:28:05 AM com.google.api.client.util.LoggingByteArrayOutputStream close
CONFIG: {
"error" : "unauthorized_client"
}
400 Bad Request
{
"error" : "unauthorized_client"
}
在谷歌公司联系人的帮助下解决了这个问题-
在授权方法中
而不是
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
从云控制台使用您的google帐户用户名
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("myemail@mycompany.com");
}
在此更改之后,如果您运行代码,它将弹出一个浏览器窗口,并要求您授权访问项目和宾果!
我读了很多关于正确的超文本传输协议状态代码返回客户端请求错误的帖子和文章。其他人建议使用400,因为它在RFC 7231中被重新定义了,尽管我不确定给出的例子是否涵盖了我脑海中所有的客户端错误,因为这些例子是语法性的。 400(错误请求)状态代码表示服务器无法或将不会处理请求,原因是被认为是客户端错误(例如,格式错误的请求语法、无效的请求 消息帧或欺骗性请求路由)。 我确实在rfc 7231的附录
在我的代码中,我试图反复构建一个客户端,并向远程endpoint发送一个输入流: 输入流包含消息的xml正文。我注意到在第一次迭代中。。。请求成功第二次迭代响应为400错误请求。。。第三个400错误请求。。。。等等。。。 如果我将输入流更改为字符串...获取真实的xml并用PUT方法发送它。 有什么区别?为什么它可以处理字符串而不能处理输入流?(似乎inputstream在第一次迭代后会发生变化)
我有一个基于Spring Web model view controller(MVC)框架的项目。Spring Web模型-视图-控制器(MVC)框架的版本是3.2.8 我有这个控制器 这个URL一切正常:
目前从Angular JS controller中,我试图将JSON数据发送到后端服务。但是我有400个错误的请求错误。 在Controller中,我试图通过http服务发送数据,如下所示:
我可以捕获400的错误代码并输出一个自定义错误,但我肯定我一定做错了,从Mailchimp API得到如此无益的响应? 非常感谢,安迪和所有的帮助都很感激。
我试图使用Gmail API将用户设置应用到Gmail帐户,但它不断返回错误400错误请求。 我可以看到错误代码在Gmail API控制台,它来自我的服务号,所以代码不可能是如此错误,但它让我发疯,只是不能找出什么是错误的。 如果有人能给我指出正确的方向,我会非常感激。