我有一个Google表单,要求用户通过Google登录才能访问该表单。如何通过api访问表单?我已经尝试通过示例驱动器api gist(上传照片的那个)登录后向表单发送一个get请求,但它仍然给我一个禁止的响应代码。
这是Google在GoogleSignIn上的快速入门:
// [START configure_signin]
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope("https://www.googleapis.com/auth/drive"), new Scope("https://www.googleapis.com/auth/plus.login"), new Scope("https://www.googleapis.com/auth/plus.me"), new Scope("https://www.googleapis.com/auth/forms"))
// [END configure_signin]
// [START build_client]
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mGoogleApiClient.connect();
// [END build_client]
这是来自尝试连接到表单的AsyncTask(需要首先登录:
public class RetrieveTokenTask extends AsyncTask<String, Void, String> {
Context mContext;
public RetrieveTokenTask(Context context){
mContext = context;
}
@Override
protected String doInBackground(String... email) {
try{
URL api = new URL("https://docs.google.com/forms/d/___FORMID/formResponse?entry.209165355=1&entry.1433596447=1&entry.2073901950=1");
HttpURLConnection conn = (HttpURLConnection) api.openConnection();
conn.setRequestMethod("POST");
conn.setInstanceFollowRedirects(true);
conn.setRequestProperty("Authorization","Bearer " + GoogleAuthUtil.getToken(mContext,email[0],"oauth2:https://www.googleapis.com/auth/forms"));
Log.wtf("Response", conn.getResponseCode()+"-"+conn.getResponseMessage());
return null;
}catch(Exception e ){
Log.wtf("FAILED",e.getMessage());
}
return null;
}
}